Magento uses cache at different levels in the system. These can be seen in the admin area under System -> Cache Management
.
The main model responsible for Magento’s cache is the Mage_Core_Model_Cache
class. You can retrieve an instance of the class using the below line of code.
Mage::app()->getCache();
The following methods can be found in the class.
load()
– Load data from the cache by ID.save()
– Save data to the cache.remove()
– Remove data from the cache by ID.clean()
– Clean cache data by tag.flush()
– Flush cached data.We can save our own data in the cache by doing the following:
$cache = Mage::app()->getCache(); $cache->save("My cached text", "custom_cache", array(), 60*60*24); echo $cache->load("custom_cache");
The save()
method takes four parameters: $data
, $id
, $tags
and $lifetime
.
The $lifetime
, is of course the amount of time in seconds that the value will exist in the cache before being removed. Setting this value to null or omitting the parameter, the value will remain in the cache until removed.
public function save($data, $id, $tags = array(), $lifeTime = null) { if ($this->_disallowSave) { return true; } /** * Add global magento cache tag to all cached data exclude config cache */ if (!in_array(Mage_Core_Model_Config::CACHE_TAG, $tags)) { $tags[] = Mage_Core_Model_App::CACHE_TAG; } return $this->getFrontend()->save((string)$data, $this->_id($id), $this->_tags($tags), $lifeTime); }
The load()
method just takes the one argument which is the ID of the data saved in the cache.
public function load($id) { return $this->getFrontend()->load($this->_id($id)); }
There are two buttons on the cache management page that can be used to flush the cache. One is Flush Magento Cache
and the other is Flush Cache Storage
.
Flush Magento Cache
will flush the cache storage that Magento is aware of.
public function flushSystemAction() { Mage::app()->cleanCache(); Mage::dispatchEvent('adminhtml_cache_flush_system'); $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The Magento cache storage has been flushed.")); $this->_redirect('*/*'); }</code></pre> <code>Flush Cache Storage</code> flushes cache including non-Magento cache such as APC. [php title="file: app/code/core/Mage/Adminhtml/CacheController.php public function flushAllAction() { Mage::dispatchEvent('adminhtml_cache_flush_all'); Mage::app()->getCacheInstance()->flush(); $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__("The cache storage has been flushed.")); $this->_redirect('*/*'); }
Cache types, like with most things in Magento, are configured in Magento’s config.xml
files.
<config> <global> .... <cache> <types> <config translate="label,description" module="core"> <label>Configuration</label> <description>System(config.xml, local.xml) and modules configuration files(config.xml).</description> <tags>CONFIG</tags> </config> <layout translate="label,description" module="core"> <label>Layouts</label> <description>Layout building instructions.</description> <tags>LAYOUT_GENERAL_CACHE_TAG</tags> </layout> <block_html translate="label,description" module="core"> <label>Blocks HTML output</label> <description>Page blocks HTML.</description> <tags>BLOCK_HTML</tags> </block_html> <translate translate="label,description" module="core"> <label>Translations</label> <description>Translation files.</description> <tags>TRANSLATE</tags> </translate> <collections translate="label,description" module="core"> <label>Collections Data</label> <description>Collection data files.</description> <tags>COLLECTION_DATA</tags> </collections> </types> </cache> .... </global> .... </config>
This means that if developing a custom cache module, you would need to declare your cache type similarly to how Magento declare theirs.
Note: This article is based on Magento Community/Open Source version 1.9.