magento

Magento Caching

How to cache custom data into Magento

const CACHE_TAG_NAMESPACE_MODULE = "YOUR_MODULES_CACHE_TAGS";
$cacheGroup = 'namespace_module';
$useCache = Mage::app()->useCache($cacheGroup);
if (true === $useCache) {    
    $cacheId = 'unique_name';
    if ($cacheContent = Mage::app()->loadCache($cacheId)) {
        $html = $cacheContent;
        return $html;
    } else {
        try {
            $cacheContent = $html;
            $tags = array(model::CACHE_TAG_NAMESPACE_MODULE);
            $lifetime = Mage::getStoreConfig('core/cache/lifetime');
            Mage::app()->saveCache($cacheContent, $cacheId, $tags, $lifetime);
        } catch (Exception $e) {
            // Exception = no caching
            Mage::logException($e);
        }
        return $html;
   }
}
// Default:
return $html;

Clean cache by cache ID

Mage::app()->removeCache($cacheId); 

Flush all Magento cache entries

Mage::app()->cleanCache()

or:

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

Use Redis as a cache backend

Redis configuration:

  1. Install redis (2.4+ required)

  2. Install phpredis

  3. Install the Magento extension Cm_Cache_Backend_Redis (only for Magento 1.7 and below)

  4. Edit your app/etc/local.xml:

    ... Cm_Cache_Backend_Redis 127.0.0.1 6379 0 0 1 0 1 1 20480 gzip ...

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow