dongyi9330 2013-03-27 10:43 采纳率: 100%
浏览 63
已采纳

如何通过密钥刷新Magento缓存?

I would like to refresh my extension cache when Magento Collections Data Collection data files. COLLECTION_DATA cache is refreshed and also for other events, which cleans this cache.

I have a custom class, the main parts are:

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

So, my target is to refresh/clean this cache too when Mage_Catalog_Model_Category::CACHE_TAG is cleaned. How is this possible?

UPDATE #1

When I have used

$cache = Mage::app()->getCacheInstance();

instead of

$cache = Mage::app()->getCache();

The clean started to work. When I have a category, the list refreshed, but if nothing happened, it stayed cache. Also could someone explain why is this possible with this changing?

Solution

...
$cache->save(serialize($this->tmpAllItems),$key,array('my_cache_tag'));
....

Then clear:

    $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);
            }
        }
    }
  • 写回答

4条回答 默认 最新

  • dongli2000 2013-11-18 04:01
    关注

    i found a solution for this problem, i use observer when click refresh cache in admin panel you can see details at my blog

    step1: create observer with event when click refresh in admin panel:

        <events>
       <adminhtml_cache_refresh_type>
        <observers>
         <yournamespace_yourextenstion_model_observer>
          <type>singleton</type>
          <class>YourNameSpace_YourExtenstion_Model_Observer</class>
          <method>adminhtml_cache_refresh_type</method>
         </yournamespace_yourextenstion_model_observer>
        </observers>
       </adminhtml_cache_refresh_type>
      </events
    

    now, we can create file name as: Observer.php follow this path: YourNameSpace/YourExtenstion/Model/Observer.php and add this code in to Observer.php

    this is content for observer.php file

    class YourNameSpace_YourExtenstion_Model_Observer
    {
     public function adminhtml_cache_refresh_type($observer)
     {
      $request = Mage::app()--->getRequest()->getPost('types');
      if(in_array('ee_content', $request))
      {
       $cache = Mage::app()->getCache();
       $tags = $cache->getTags();
       $ee_content = '';
       foreach($tags as $tag)
       {
        if(strpos($tag, 'ee_content'))
        {
         $ee_content = $tag;
        }
       }
       if($ee_content !='')
       {
    
    
        $ids = $cache->getIdsMatchingAnyTags(array($ee_content));
        foreach($ids as $id)
        {
         $cache->remove($id);
        }
       }
    
      }
     }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看