We continue posting series of blog articles, dedicated to our clients and their businesses, operating on BelVG extensions. We strongly believe that success of any eCommerce development company is determined by the amount of profit, gained by store merchants. So we dig deeper to find out, whether our customers are satisfied, and then share their answers with you.
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:
<mycache translate="label,description" module="mycache">
Magento uses the following cache model: Mage::getModel(‘core/cache’) and 4 main methods:
- save($value, $key, $tags = array(), $lifeTime=null);
- 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())
Removing all the data which is defined by a specific tag: for example ‘MYCACHE_CACHE’