简体   繁体   English

无法删除使用beginCache创建的缓存项目

[英]Can not delete cached item created with beginCache

I have a fragment that I'm caching because there are a lot of database hits to generate the content. 我有一个要缓存的片段,因为有很多数据库命中来生成内容。

main.php: main.php:

    'cache'=>array(
            'class'=>'system.caching.CFileCache'
    )

view.php view.php

<?php if($this->beginCache("my_id", array('duration' => 315360000))) { ?>
   // lots of heavy stuff
<?php $this->endCache(); } ?>

My cached fragment only changes when I adjust the configuration in a database table. 仅当我调整数据库表中的配置时,缓存的片段才会更改。 I'm trying to find a way to delete the cached item using the "my_id". 我正在尝试找到一种使用“ my_id”删除缓存项的方法。 The Yii documentation is somewhat misleading because when it talks about beginCache it refers to the key ("my_id"), but when it talks about deleting items from the cache it also talks about the key, but it is not the same key! Yii文档在某种程度上具有误导性,因为当谈论beginCache它指的是键(“ my_id”),但是当谈论从缓存中删除项目时,它也谈论键,但它不是同一个键! beginCache is essentially a wrapper for an COutputCache widget and so "my_id" is not the key used in the cache. beginCache本质上是COutputCache小部件的包装,因此“ my_id”不是高速缓存中使用的键。

Does anyone know how to convert "my_id" into a cache-friendly key so that I can delete that specific item from the cache. 有谁知道如何将“ my_id”转换为对缓存友好的键,以便我可以从缓存中删除该特定项。

I've tried extending from CFileCache but my key is generated from COutputCache which uses CFileCache and I don't think there is a back route. 我尝试从CFileCache扩展,但是我的密钥是从使用CFileCache COutputCache生成的,我认为没有反向路由。 I have also tried beginning a widget with an overloaded version of COutputCache which works but just seems like a really nasty hack. 我也尝试过使用COutputCache的重载版本开始一个小部件,该小版本虽然可以正常工作,但看起来确实很讨厌。

    $properties = array();
    $properties['id']=Yii::app()->params["cache_name_matrix"];
    $cache=$this->beginWidget('MyCOutputCache',$properties);
    $key = $cache->getKeyHack();

My current work around is flush the whole cache but this gets rid of everything and seems a little bit heavy handed. 我当前的解决方法是刷新整个缓存,但是这摆脱了所有问题,似乎有点费力。

Any ideas? 有任何想法吗?

COutputCache calculates the cache key using protected method called getCacheKey which internally calls getBaseCacheKey and then depending on specified variations transforms it. COutputCache使用称为getCacheKey的受保护方法计算缓存密钥,该方法在内部调用getBaseCacheKey ,然后根据指定的变体对其进行转换。 In your, the simplest case, with no variations, I believe it will end up with something like 'Yii.COutputCache.my_id.......' where 'Yii.COutputCache.' 在您最简单的情况下,没有任何变化,我相信它将以“ Yii.COutputCache.my_id .......”之类的名字结尾,其中“ Yii.COutputCache”。 is COutputCache::CACHE_KEY_PREFIX. 是COutputCache :: CACHE_KEY_PREFIX。 Take a look at the code of this method. 看一下此方法的代码

So, knowing this, you technically can delete your fragment manually. 因此,知道这一点后,您可以从技术上手动删除片段。 But I strongly encourage you to take a look at mechanism of dependencies described in the manual . 但是我强烈建议您看一下手册中描述的依赖机制。 You said you invalidate this fragment if something changes in your database, so probably CDbCacheDependency is what you need. 您说如果数据库中发生某些更改,则使该片段无效,因此可能需要CDbCacheDependency

Yii2 try this Yii2试试这个

$key = Array ('yii\widgets\FragmentCache','key_id');
Yii::$app->cache->delete($key);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM