How to clear cache in yii2 AR?
2 answers
You can modify this code to use the data cache instead of the request cache so that you can use a unique key.
$data = $cache->get('customer' . $id);
if ($data === false) {
$data = Customer::findOne($id);
$cache->set('customer' . $id, $data, 60 * 60 * 24 * 4);
}
or since version 2.0.11:
$data = $cache->getOrSet('customer' . $id, function () use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
So now you can use
$cache->delete('customer' . $id);
+3
source to share
you can use flush
for globals.
Yii::$app->cache->flush();
you can use TagDependency
:
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4, new TagDependency(['tags'=>'customer']));
//to flush
TagDependency::invalidate(Yii::$app->cache, 'customer');
For more information check here
0
source to share