Magento EE – Punch Hole in Full Page Cache
Magento is slow, it’s a truth . because of its slowness, Magento have some cache mechanisms to make your site faster, so your customers will be happy. Many people choose to use all of caching options available. according to their opinions, the best cache solution for Magento is Full Page Cache (FPC), and it’s one feature from Magento Enterprise.
implementing FPC means you will have all your page content cached. it’s good for speed, but what will happen when you have several block that need to be updated after each request, or may be you want a shorter cache life for it ? The following tutorial will show you how to punch some holes in your FPC pages so you can solve above problems.
1. i modified layout xml file to set a name to block
<block type="core/text_list" name="top.menu" as="topMenu" translate="label"> <label>Navigation Bar</label> <!--<block type="core/template" template="catalogevent/lister_navigation.phtml" /> --> <block type="core/template" name="catalog.event" template="catalogevent/lister_navigation.phtml" /> </block>
2. then i created a new cache.xml file in etc folder under module
<?xml version="1.0" encoding="UTF-8"?> <config> <placeholders> <catalog_navigation> <block>core/template</block> <name>catalog.event</name> <placeholder>CATALOG_NAVIGATION_FPC_CUSTOM</placeholder> <container>Enterprise_PageCache_Model_Container_Catalognavigation</container> <cache_lifetime>10</cache_lifetime> </catalog_navigation> </placeholders> </config>
Notice in the above tags, the tagname should be any unique cache tagname, in our case its ‘myblock_someaction’. The block tag and the name tag should have the block name and the name as it appears in the layout in design. The placeholder tag should have any unique string. The container needs to contain the model which actually defines the behavior of the block cache. And finally, the cache_lifetime is the time till the cache will be valid. Now, all that matters is the model which defines the behavior of the cache and we hope to look it in details.
finally, i created a new container for custom FPC cache name (in this case Punchhole.php) with following content
<?php class Wage_Fpc_Model_Punchhole extends Enterprise_PageCache_Model_Container_Abstract { /** * Get customer identifier from cookies * * @return string */ protected function _getIdentifier() { return $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, ''); } /** * Get cache identifier * * @return string */ protected function _getCacheId() { return 'CATALOG_NAVIGATION_FPC_CUSTOM' . md5($this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier()); } /** * Render block content * * @return string */ protected function _renderBlock() { $blockClass = $this->_placeholder->getAttribute('block'); $template = $this->_placeholder->getAttribute('template'); $block = new $blockClass; $block->setTemplate($template); return $block->toHtml(); } }
i also set cache_lifetime to 10 seconds, if you want to make it work well with default FPC, you can set it to 86400 (1 day).
Thank you for a great post!
We can also try Magento Full Page Cache extension to reduce database loading in the server
This will help to enhance customer experience when visiting Magento sites because loading time is reduces and site speed is increased effectively.
http://bsscommerce.com/magento-full-page-cache.html
– New multi-level cache to serve pages even faster
– Support MULTIPLE currencies/stores/languages
– Support mobile themes
– Easily access to cache custom pages