D
D
Dmitry2019-03-10 15:30:26
Yii
Dmitry, 2019-03-10 15:30:26

Yii2. Why doesn't TagDependency::invalidate() work?

Hello.
Please help me to deal with conditional caching (TagDependence) in Yii2. I am using memcached.
I want to cache the user's personal data (full name).
The logic is as follows:
1) in the UsersAuthComponent component, in the getUserById method, I set the cache by TagDependency.

public function getUserById($id)
    { // получить запись (ActiveRecord) пользователя User по id
        return $this->getModel()::find()
            ->andWhere(['id' => $id])
            ->cache(null, new TagDependency(['tags' => 'current_user' . \Yii::$app->user->id]))
            ->one();
}

2) then, if new data is entered into the full name editing form, the editUser() method of the same UsersAuthComponent component is called, in which the condition on the tag is deleted - TagDependency::invalidate.
public function editUser($model)
    { // обновить данные пользователя
        if (!$model->validate(['fio'])) {
            return false;
        }
        if (!$model->update()) {
            return false;
        }

        TagDependency::invalidate(\Yii::$app->cache, 'current_user'. \Yii::$app->user->id);
        return true;
    }

I understand this so that when the user's full name field is updated during the editing process, the editUser method will be called, which will overwrite the dependency on the tag, after which the cache will be deleted and a new full name will be displayed from the database.
In fact, everything happens differently - the field is updated in the database, but not on the user page (displayed from the cache).
Tell me, please, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question