The Basics Concepts of Magento Cache – How To Use Cache

Magento can manage to cache at many levels in the system, most of which are transparent to developers who are not familiar with development in depth. As the complexity of your work with Magento has increased, it is imperative that you ultimately require caching yourself.

Caching Concepts

cache in Magento looks like many other systems that it stores a key-value. If we want to save $data in the cache, we need to treat them with a name, $key. When we need to retrieve $data later, we will do it with the same $key.

$data stored in the cache can also be tagged with metadata. This allows the logical group of cache entries to delete bulk.

Magento provides an interface to enable, disable and empty the cache in the administration area. Each row in the “Cache Administration” table represents a cache tag. When updating one of these rows, we are deleting all the cache entries with a certain tag.

The Cache API

The model that controls our interface with the caching subsystem is Mage_Core_Model_Cache. Normally we access the cache through an already instanced object accessible in Mage::app()->getCache().

The implementation of the cache memory is hidden for us, the interface that is provided to us consists (almost entirely) of the following methods:

save($value, $key, $tags = array(), $lifeTime=null)

load($key)

remove($key)

clean($tags = array()

The actual backend used for caching can be a number of mechanisms from the built-in flat file approach (here’s a more efficient algorithm) to Memcached, APC, or even Redis for the hardcore.

Saving Data To Cache


<?php
$data = array(1,2,3,4,5,7,8);  //any data which you want to save to cache
$id = 'my_mod_id_key'; //unique id for your cache data
$tags = array('collection'); //cache tags will be explain later in detail
$lifetime = false; //false means infinity, or you can specify number of seconds
$priority = 8; // number between 0-9, used by few backend cache models
 
Mage::app()->saveCache($data,$id,$tags,$lifetime,$priority);
?>

Retrieving Data
Mage::app()->loadCache($id);

Deleting Cache
Mage::app()->removeCache($id);
Checking If Cache Is Enabled
Mage::app()->useCache(‘collection’); //check if cache group is enabled or not

What are Cache Tags
Magento has an important concept of cache tags in its caching system. Magento basically uses tags to group cache data and then we delete a specific tag group from admin, it removes all the data specific to that group of tags.
The different cache tags we have are “block_html”, “collections”, “config”, “config_api”, “config_api2”, “eav”, “layout”, “translate”, “mage”
These are the same configuration tags that we see when we go to System -> Cache Management
Then, when you delete a single magenta cache tag, it will erase all the cache data related to that tag.

I found how to proceed  cache in Magento:

$cacheId = ‘my_cache_id';

if (Mage::app()->useCache(‘block_html)) {
if (false !== ($data = Mage::app()->loadCache($cacheId))) {
/* PROCESS CACHE */
$mydatas = unserialize($data);
} else {
/* PROCESS NO CACHE */
$mydatas = “toto”;
Mage::app()->saveCache(serialize($fapiResponse), $cacheId, array(‘block_html’), 86400);
}
} else {
/* PROCESS NO CACHE */
}

If you are in a block inherits Mage_Core_Block_Abstract you can use :
self::CACHE_GROUP instead of “block_html”
$this->getCacheTags instead of array(‘block_html’)

Leave a Reply

Your email address will not be published. Required fields are marked *