Friday, August 16, 2013

Magento : How to cache custom data into Magento

Use following code in configuration file as
<global>
    <cache>
        <types>
            <namespace_module module="namespace_module" translate="label description">
                <label>Your modules cache label</label>
                <description>Description of your modules cache</description>
                <tags>YOUR_MODULES_CACHE_TAGS</tags>
            </namespace_module>
        </types>
    </cache>
</global>
Then create a model class file witha variable and a method as
const CACHE_TAG_NAMESPACE_MODULE = 'YOUR_MODULES_CACHE_TAGS';
$cacheGroup = 'namespace_module';
$useCache = Mage::app()->useCache($cacheGroup);
if (true === $useCache) {
    // Cache is active
    $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;
    }
} else {
    // Cache is not active
    return $html;
}

Also we can following code for clean the cache
Mage::app()->removeCache($cacheId);


By default Magento uses its file system to save cache (files located in the directory var/cache/).
Another option is the database, which is slower then the files option mostly because the database is a heavily used resource for Magento instances anyway. Then there are storage schemes that use RAM (which are much faster), e.g. APC (shared memory) or memcached (a networked caching server) or Redis.

The Magento cache is organized by tags, this means you have cache entries which belongs to a cache group.

APC is an opcode cache for PHP. It is a framework for caching and optimizing PHP code. APC also serves as a user cache. APC cache is stored in memory where performance gains over file system cache are huge.
There following step to configure APC in Magento
1. Install php5-apc package (like sudo apt-get install php5-apc)
2. Edit and configure setting in apc.ini
3. Add the following code between the global tags in app/etc/local.xml.
<global>
<cache>
            <backend>apc</backend>
            <prefix>mystore_</prefix>
        </cache>
</global>

There following step to configure Memcache as a Fast Backend in Magento
1. Install Memcache as a PHP extension (apt-get install memcached )
2. And enable it in your php.ini
3. Add the following code between the global tags in app/etc/local.xml.
<cache>
<backend>memcached</backend>
<!-- apc / memcached / empty=file -->
<memcached>
<!-- memcached cache backend related config -->
<servers>
<!-- any number of server nodes can be included -->
<server>
<host><![CDATA[127.0.0.1]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[1]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>

There following step to configure Redis as a Fast Backend in Magento
1. Install redis (2.4+ required)
2. Install phpredis
3. Install the magento extension “Cm_Cache_Backend_Redis”
4. Add the following code between the global tags in app/etc/local.xml.
<cache>                                              
<backend>Zend_Cache_Backend_Redis</backend>                            <slow_backend>database</slow_backend>                                  <slow_backend_store_data>0</slow_backend_store_data>                   <auto_refresh_fast_cache>0</auto_refresh_fast_cache>
<backend_options>                                              
<server><![CDATA[127.0.0.1]]></server>
<port><![CDATA[11211]]></port>                                              
<database>database</database>                                        
<use_redisent>0</use_redisent>  <!-- 0 for phpredis, 1 for redisent -->                                                <automatic_cleaning_factor>0</automatic_cleaning_factor> <!-- optional, 20000 is the default, 0 disables auto clean -->
</backend_options>
</cache>

1 comment: