drlndkhib08556095 2014-10-28 20:38
浏览 94

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

I have a fragment that I'm caching because there are a lot of database hits to generate the content.

main.php:

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

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". 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! beginCache is essentially a wrapper for an COutputCache widget and so "my_id" is not the key used in the cache.

Does anyone know how to convert "my_id" into a cache-friendly key so that I can delete that specific item from the cache.

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. I have also tried beginning a widget with an overloaded version of COutputCache which works but just seems like a really nasty hack.

    $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?

  • 写回答

2条回答 默认 最新

  • duanheye7909 2014-10-28 21:00
    关注

    COutputCache calculates the cache key using protected method called getCacheKey which internally calls getBaseCacheKey and then depending on specified variations transforms it. In your, the simplest case, with no variations, I believe it will end up with something like 'Yii.COutputCache.my_id.......' where 'Yii.COutputCache.' is 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?