The Basics – How To Use Cache
In this section we will see how to use cache in our custom modules. Suppose you have a custom module and wanted to save some data in magento cache, lets see how to do it.
Saving Data To Cache
1
2
3
4
5
6
7
| $array = array (1,2,3,4); //any data which you want to save to cache $id = 'my_mod_id' ; //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( $ array, $id , $tags , $lifetime , $priority ); |
Retrieving Data
1
| Mage::app()->loadCache( $id ); |
Deleting Cache
1
| Mage::app()->removeCache( $id ); |
Checking If Cache Is Enabled
1
| Mage::app()->useCache( 'collection' ); //check if cache group is enabled or not |
above are some basic caching function we can use in magento.
Cache Tags
Magento has an important concept cache tags in its caching system. Magento basically uses tags to group cache data together and then we clear a specific tag group from admin, it removes all data specific to that tag group.
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 which we see when we go to System -> Cache Management
So when you clear a single cache tag in magento, it will clear all cache data related to that tag.
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 which we see when we go to System -> Cache Management
So when you clear a single cache tag in magento, it will clear all cache data related to that tag.
The “mage” tag is a special tag, it automatically gets added when you save anything in magento except when you save configuration data.
Template Caching
Lets look at how magento cache’s your blocks and phtml files.
All magento block extend “Mage_Core_Block_Abstract” class which has the “toHtml()” function. This function is called when the template of a particular block is displayed. In this function, we have the
All magento block extend “Mage_Core_Block_Abstract” class which has the “toHtml()” function. This function is called when the template of a particular block is displayed. In this function, we have the
$this->_loadCache()
which check if cache is enabled from admin and if cache data is present. If data is present in cache, it directly displays it else the toHtml() function “core/template” is called. So this is how all your magento blocks automatically get cache and all blocks use “block_html” as cache tag.Magento Admin Cache Operations
In admin we have various cache operations we can do, lets look at each of them and see what they do.
Flush Magento Cache: This operation calls the function
This function clears all cache tags data.
Flush Cache Storage: This function simply clear up the cache based on caching system. e.g in file storage this will delete all data in “var/cache” folder and in memcache it will delete all data in memcache.
Flush Magento Cache: This operation calls the function
Mage::app()->cleanCache();
function.This function clears all cache tags data.
Flush Cache Storage: This function simply clear up the cache based on caching system. e.g in file storage this will delete all data in “var/cache” folder and in memcache it will delete all data in memcache.