如何通过密钥刷新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);
            }
        }
    }

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);
    }
   }

  }
 }
} 

getCacheInstance() will get you core cache model instance, while getCache() will get for you cache object.

To clean everything, by getCacheInstance() and getCache(), you need to use it like below:

Mage::app()->getCacheInstance()->flush();
Mage::app()->getCache()->clean();

To clean your custom cache type, you will use:

Mage::app()->getCacheInstance()->cleanType('your_custom_cache_type');

Hi You can create your own dispatch event by using Mage::dispatchEvent method. Override your Model file Mage_Catalog_Model_Category and register a new dispatcher over there.

check event application_clean_cache

when clear cache like - Mage::app()->cleanCache();

or when system saves product etc...

and take a look on event - adminhtml_cache_refresh_type when clearing cache via admin panel

and your code in observer

Mage::app()->getCache()->clean('all', array('my_tag'));

and need add some logic to Observer, for check need refresh cache or not