Get Free Audit

Using Default Magento Cache

May 26, 2014

2199 Andrei Danilchyk

Using Default Magento Cache

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:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

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’

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

4 Comments

  1. 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.

  2. > 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

  3. 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!

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *