Question

I'm developing couple of Magento extensions that change the content of the sites dynamically (alternative products sorting, different related items, etc), based on things like user and time.

These features work perfectly on non cached sites, but unfortunately for most sites there's some sort of caching mechanism that prevents my extension changes to take effect.

For some sites I can ask the owner to mark specific blocks as uncacheable (like the related products for example), but apparently its not always possible, and for other features, removing the cache on the entire collection will make the site too slow.

So I wanted to ask how do you guys handle caching in your extensions, when you want to change site's content programatically (eg when you can't operate with cache even if its cleared often)? what are the tricks and techniques you use to overcome this problem?

So far I managed to do some hybrids between magento code and JavaScript with ajax and jquery to change the cached content after page is loaded, but that's less scalable, uglier, and feels wrong. I want to reduce the js to minimum.

Am I doomed to make all my extensions more ajax and js based instead of using all the wonderful tools Magento provide me if I want to work on cached content?? :/

Thanks!

Was it helpful?

Solution

3 Main things drive the caching invalidation: cache_key, cache_lifetime and cache_tags. These also help drive FPC invalidation and hole punching. Aoe_TemplateHints is a great module to see if your blocks are caching properly.

cache_tags is meant as a means of grouping blocks to help invalidate other content that may contain a block that has been invalidated so it can be regenerated.

cache_lifetime I think is self explanatory. It won't function properly if no tag, key or container has been put in place properly.

cache_key is what is checked to determine if any data has changed within it to rebuild its content and invalidate it. Normally this is md5 hashed so it's easy to detect any changes of content.

Normally you want to add the following data to your modules Blocks:

protected function _construct()
{
    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Cms_Model_Block::CACHE_TAG),
        'cache_key'      => 'CACHE_KEY',
    ));
}

Note If you set cache_lifetime to 0 (or any other value which PHP evaluates to false) then the cache will actually last 7200 seconds (2 hours). You can also specify this data via the XML layout as well.

In order to hole punch the block with Magento's FPC enabled you'll want to define a placeholder. In your modules etc/ folder you would create a cache.xml

<config>
    <placeholders>
        <namespace_module>
            <block>namespace_module/something</block>
            <placeholder>NAMESPACE_MODULE_UNIQUE_STRING</placeholder>
            <container>Namespace_Module_Model_Pagecache_Container</container>
            <cache_lifetime>86400</cache_lifetime>
        </namespace_module>
    </placeholders>
</config>

Conclusion:

It is difficult to support every caching system out there such as Varnish, Tiny Bricks Warp, Lesti::FPC and the countless other FPC modules in the community. Most should follow the same principles of looking for data in your blocks.

Further reading:

OTHER TIPS

You should clean cache programatically:

$tags = Mage::app()->getCacheInstance()->cleanType($type);
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
$updatedTypes++;

Possible types are as follows:

  • config
  • layout
  • block_html
  • translate
  • collections
  • eav
  • config_api
  • config_api2

And these can be returned by calling:

Mage::app()->getCacheInstance()->getTypes()
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top