Answer the question
In order to leave comments, you need to log in
How to flush Yii2 cache?
How to reset such caching?
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
Answer the question
In order to leave comments, you need to log in
$data = $cache->getOrSet('customer' . $id, function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
$cache->delete('customer' . $id);
use yii\caching\DbDependency to determine if a record has changed
It's best to use Tags for this cache, so it's possible to do one cache flush for a whole bunch of keys and queries
$tag = 'Customer-' . $id; // готовим таг
$ttl = 60 * 60 * 24 * 4; // готовим время жизни кеша
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, $ttl, new TagDependency(['tags' => $tag])); // Пометка тагом данного кеша
TagDependency::invalidate(Yii::$app->cache,$tag); // Очистка всех кешей с данным тагом
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question