我想在 Magento Collections Data Collection 数据文件时刷新我的扩展缓存。COLLECTION_DATA 缓存被刷新,其他事件也会刷新,这会清理这个缓存。
我有一个自定义类,主要部分是:
$this->_usecache = Mage::app()->useCache('collections');
if ($this->_usecache){
$cache = Mage::app()->getCache();
$key = "mycategory".$this->_config['rootid'];
$this->tmpAllItems = $cache->load($key);
} else {
$this->tmpAllItems = false;
}
if ($this->tmpAllItems === false){
$this->tmpAllItems = array();
$model = Mage::getModel('catalog/category');
$categories = $model->getCategories($this->_config['rootid'], 0, true, false, true);
$k = array_keys($categories->getNodes());
$parent = $categories[$k[0]]->getParent();
$this -> treeToFlat($parent);
if ($this->_usecache)
{
$cache->save(serialize($this->tmpAllItems),
$key,
array(Mage_Catalog_Model_Category::CACHE_TAG,
Mage_Core_Model_App::CACHE_TAG)
);
} else {
$this->tmpAllItems = unserialize($this->tmpAllItems);
}
return $this->tmpAllItems;
所以,我的目标是在清理 Mage_Catalog_Model_Category::CACHE_TAG 时也刷新/清理这个缓存。这怎么可能?
更新#1
当我用过
$cache = Mage::app()->getCacheInstance();
代替
$cache = Mage::app()->getCache();
清洁开始工作。当我有一个类别时,列表会刷新,但如果什么也没发生,它会保持缓存。也有人可以解释为什么这种变化会发生这种情况?
解决方案
...
$cache->save(serialize($this->tmpAllItems),$key,array('my_cache_tag'));
....
然后明确:
$cache = Mage::app()->getCache(); foreach($cache->getTags() as $tag){ if(strpos($tag, 'my_cache_tag')){ $ids = $cache->getIdsMatchingAnyTags(array($tag)); foreach($ids as $id){ $cache->remove($id); } } }