How to implement magento cache hole punching for s

2019-05-21 08:54发布

Im using Magento EE version 1.12 with Full page cache enabled

a) my product detail page is cached b) as a result my shopping cart in this page doesn't show dynamic item count c) so i am not able to show valid cart item count in my product detail page

steps i followed

1) I created a block and called from header.phtml 2) trying to make that topcart.phtml block not to be cached

As im a newbie in magento , i got some links for cache hole punching

I followed below links but no success

my file structure

app- code - local - Enterprise - PageCache ->etc - cache.xml

and PageCache - model -container - TopCart.php

code as shown below

help link one

help link two

link three

i created files cache.xml and cart.php container file

 <page_html_topcart>
        <block>page/html_topcart</block>
        <name>topcart</name>
        <placeholder>PAGE_HTML_HEADER_CART</placeholder>
        <container>Enterprise_PageCache_Model_Container_TopCart</container>
        <cache_lifetime>36400</cache_lifetime>
    </page_html_topcart>

this is my topcart.php container file looks like

  protected function _getIdentifier()
{
    $cacheId = $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '')
        . '_'
        . $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER_LOGGED_IN, '');

    return $cacheId;
}

 protected function _getCacheId()
{
    return 'CONTAINER_TOPCART_' . md5($this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier());
}

protected function _renderBlock()
{
    $block = $this->_getPlaceHolderBlock(); //('page/html_header_cart');
    Mage::dispatchEvent('render_block', array('block' => $block, 'placeholder' => $this->_placeholder));
    return $block->toHtml();
}

kindly help me out with useful links and step

1条回答
可以哭但决不认输i
2楼-- · 2019-05-21 09:38

I faced the same issue. So, i think, the problem is, that we don't have cachable template in this case. So if you cache it the way you did (and as I did, too), you end up in a base64 encoded list of links in your cache file. To validate that, I uncompressed the files in var/full_page_cache - and here we go: the cart count is cached and won't be changed even if your cart changes, and it would be not replaceable on server side (at least not in a clean way).

The reason for this behaviour is a simple one: For FPC, you render templates only, passing some values. But the template only renders a list in that special case, accessing only one block method (getLinks). In your layout xml files, you will find some calls of "addLink", which feeds that block, that's why all the results become base64 encoded and end up in your cache file. They are not accessable by your container.

But I think, there is a way to fix that. Just collect the links you want to be rendered and create a custom template and a custom block for that. You'll now be able to cache it in a proper way.

查看更多
登录 后发表回答