Quite often, during the process of module development or when trying to solve some tasks it is necessary to cache some data. The easiest way is to use the default Magento cache since Magento already has all necessary models and mechanisms, both for using and clearing cache.
Everything is simple: first register your cache tag (in future it will be used to clear the cache). Add the following code into config.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<global> ... <cache> <types> <mycache translate="label,description" module="mycache"> <label>MyCache Requests</label> <description>MyCache Requests</description> <tags>MYCACHE_CACHE</tags> </mycache> </types> </cache> ... </global> |
Magento uses the following cache model: Mage::getModel(‘core/cache’) and 4 main methods:
- save($value, $key, $tags = array(), $lifeTime=null);
- load($key);
- remove($key);
- clean($tags = array();
Saving to cache: $key, $tags = array(), $lifeTime=null):
Example: Mage::app()->getCache()->save(json_encode($this->results), ‘mycache_info_’ . $query, array(‘MYCACHE_CACHE’), $lifetime);
Where: $value – the data you need to cache
$key – a unique key, which can provide the cached data. As a rule it consists of a dynamic and static parts.
For example: ‘mycache_info_’ is a statics part; As a dynamic part it is possible to use the search query $query
$tags – tags that are applied for clearing cache
$lifeTime – life time in seconds
Loading from cache: load( $key)
Example: Mage::app()->getCache()->load(‘mycache_info_’ . $query);
Where: $query: a dynamic parameter ( for example search query)
Cleaning cache by key: remove( $key)
Example: Mage::app()->getCache()->remove(‘mycache_info_’ . $query);
Deleting from cache only the information defined by $key
Clearing cache by tags: clean($tags = array())
Example: Mage::app()->getCache()->clean(array(‘MYCACHE_CACHE’));
Removing all the data which is defined by a specific tag: for example ‘MYCACHE_CACHE’
Sushil Rangari,
It’s just storages for different types of cache
var/cache – storage for all default magento types of cache including custom cache which describing in this article.
var/full_page_cache – storage for magento’s module Enterprise_PageCache (only for enterpise magento) – it’s full page caching.
Hello
What actually magento stores under var/cache and var/full_page_cache
> how can you check if a cache key is enabled or not? I really need to be able to do this.
Mage::app()->useCache(); – this function is return states (enable/disable) of all types of cache in array
> what increment is the lifetime in? (milsec, seconds, minutes, hours, days)
in seconds
> testing this cache mechanism without the config.xml seems to be working fine…is the config xml only for the admin enable/disable and flush/refresh? do those things happen automatically or do you have to check for them in your code?
it’s for displaying on the admin panel (enable/disable/refresh) and creating of new type of cache
if you need to do something with this new type of cache automatically you can use magento’s observers
how can you check if a cache key is enabled or not? I really need to be able to do this.
what increment is the lifetime in? (milsec, seconds, minutes, hours, days)
testing this cache mechanism without the config.xml seems to be working fine…is the config xml only for the admin enable/disable and flush/refresh? do those things happen automatically or do you have to check for them in your code?
thanks!