如何清除当前数据的缓存?如何清除yii2 AR中的缓存?
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
我想更新
如何清除当前数据的缓存?如何清除yii2 AR中的缓存?
$result = Customer::getDb()->cache(function ($db) use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
我想更新
可以修改此代码以使用数据缓存,而不是查询缓存,以便您可以使用唯一的密钥。
$data = $cache->get('customer' . $id);
if ($data === false) {
$data = Customer::findOne($id);
$cache->set('customer' . $id, $data, 60 * 60 * 24 * 4);
}
或开始从2.0.11:
$data = $cache->getOrSet('customer' . $id, function() use ($id) {
return Customer::findOne($id);
}, 60 * 60 * 24 * 4);
所以,现在你可以使用
$cache->delete('customer' . $id);
您可以使用flush
全球后,清除客户当前数据的高速缓存。
Yii::$app->cache->flush();
您可以使用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');
有关更多信息,请here
这是帮助你的? @lolka – lalithkumar
我理解正确吗?对于当前$ id,我可以添加新的TagDependency(['tags'=>'customer-'。$ id]',然后刷新TagDependency :: invalidate(Yii :: $ app-> cache,'customer-' (',我不想冲洗所有的缓存,只有当前的$ id客户 – lolka
你试过这样吗? – lalithkumar
thnx,它的工作原理),' – lolka