Magento 1

Temporarily disable a cache type

In particular instances, it may be necessary to disable a type of layout cache. For instance, you may AJAX a call to a page which loads a layout handle for the root node and returns it as an JSON response. It may be it necessary to later load another layout handle as the root node, but with layout caches on this erroneously loads the first layout handle. The following solution will temporarily prevent the layout XML from being cached:

    protected function _disableLayoutCache()
        $cache = Mage::app()->getCacheInstance();
Magento 1


Retrieving the cache object

<?php Mage::app()->getCache();

Checking if a Cache Type is Enabled


Saving to and Retrieving from Cache

Setting $lifetime to 0 or null means the value will be cached indefinitely, alternately enter a second value.

$cache->save($value, $key, $tags = array(), $lifeTime=null);

Clearing a Cache using Tags

<?php $cache->clean($tags = array());

Clearing Cache from the Admin

To clear the custom caches from the admin, use the “Flush Cache Storage” button, as the “Flush Magento Cache” button only deals with Magento’s own cache. Obviously, this is if a custom cache type hasn’t been added to the admin using the method above.

For your custom cache type to appear in the admin, use the following XML to your module’s config file:

             <your_cache_type module="your_module" translate="label description">
                <label>Your Custom Cache Label</label>
                <description>Description of cache type.</description>

Block Caching

To change the caching methods on a block, you can change the cache key to make the block cache for a specific thing. Eg, for a category key:

<?php $block->setCacheKey($category_key);

Or, in XML:

<block type="custom/block" name="custom.block">
    <action method="setCacheKey">
        <!-- or to make it dynamic... -->
        <key helper="myhelper/getCacheKey" />

Turning off Caching for a Block



<action method=”setCacheLifetime” />

Making a Block Cache Permenantly

<?php $block->setCacheLifetime(false);

Magento’s Topmenu

Very few blocks cache by default in Magento, however the Topmenu block is set up to (presumably because of the possibly heavy queries in generating them)

Caching should be set in a block’s constructor if it’s not being set in XML. E.g, The Mage_Page_Block_Html_Topmenu‘s constructor looks like the following:

public function _construct()
        'cache_lifetime' => false,

This behaves the same as $this->setCacheLifetime(false), meaning it will cache indefinitely. A problem with the top menu is that it caches regardless of whether the page is secure or not. If a secure page loads an unsecured cache then any links to elements will cause browser warnings; to get around this, we can do something like the following in the constructor which will create a separate cache of the block for http and https:

$this->addData('cache_key', Mage::app()->getStore()->isCurrentlySecure() ? "secure" : "unsecure");

**TODO: getCacheKeyInfo **