3

See below for a rewrite of this question.

A Magento EE 1.13.1.0 site that I'm working on is having some issues with Banners (powered by widgets.) The banners are caching when FPC is turned on, and even placing the call to the banner block in an uncached placholder/container is still pulling a cached banner block.

After talking to Magento support, they basically said that they couldn't help, but that I could look into the Related Products/Upsells to see how they stay uncached.

I know that the block catalog.product.related and it's child block catalog.product.related.item are both uncached. (Or rather, that they exist in the cache but are refreshed on each page load, not that I understand the difference.) But looking at Enterprise_PageCache_Model_Container_CatalogProductItem I can't see what it is about this that keeps these blocks from caching indefinitely. Especially as it relates to Enterprise_PageCache_Model_Container_Banner, which seems very similar.

Can anyone tell me how I can get Banners to not cache at all or give me a way to pull the content for a banner from the database directly, and bypass the cache?

Edit:

The problem that I was faced with is this: In Magento EE there is a Banner system tied to the Widget system in the CMS. The homepage of the site in question had 4 banners/widgets which were assigned to blocks. Those blocks were then called in the homepage template like so:

<!-- page.xml -->
<block type="core/text_list" name="home_banner_carousel_one" as="home_banner_carousel_one">
    <label>Homepage Banner Carousel One</label>
</block>
<block type="core/text_list" name="home_banner_carousel_two" as="home_banner_carousel_two">
    <label>Homepage Banner Carousel Two</label>
</block>
<block type="core/text_list" name="home_banner_carousel_three" as="home_banner_carousel_three">
    <label>Homepage Banner Carousel Three</label>
</block>
<block type="core/text_list" name="home_banner_carousel_four" as="home_banner_carousel_four">
    <label>Homepage Banner Carousel Four</label>
</block>

<!-- page/home.phtml -->
<ul class="home-slider">
    <li><?php echo $this->getChildHtml('home_banner_carousel_one', false); ?></li>
    <li><?php echo $this->getChildHtml('home_banner_carousel_two', false); ?></a></li>
    <li><?php echo $this->getChildHtml('home_banner_carousel_three', false); ?></a></li>
    <li><?php echo $this->getChildHtml('home_banner_carousel_four', false); ?></a></li>
</ul>

The problem was that home.phtml was written to hide that section if the first carousel block was empty. Like this:

$carouselOne = $this->getChildHtml('home_banner_carousel_one', false);
if (empty($carouselOne)) {
    echo 'Nothing here';
} else {
    echo $carouselOne;
}

This was written without regard for the Full Page Cache in Magento. When that cache was turned on, this logic suddenly broke.

My solution was to move this logic into it's own block, and then try to uncache that "wrapper" block and all of the carousel blocks inside.

Simply adding <action method="setCacheLifetime" /> to the block definition in page.xml didn't have any affect on the Enterprise Full Page Cache.

  • possible duplicate of How can i disable cache for particular section or block – Sander Mangel Feb 14 '15 at 3:39
  • Both <action method="setCacheLifetime" /> and echo $this->getChildHtml('block', false); solutions from that answer have been employed, and do not defeat the Enterprise Full Page Cache. – Tyler V. Feb 14 '15 at 4:17
  • Ok, just asked around on the stackday slackgroup for someone to look into it. I retracted my closed – Sander Mangel Feb 14 '15 at 4:18
2

The best way to determine caching of blocks is to use Aoe_TemplateHints module and view the blocks in question, as it will give you the details on caching of all of your blocks.

3 Main things drive the caching invalidation: cache_key, cache_lifetime and cache_tags.

The 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.

To answer your question, the 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.

An example block class:

public function _construct(){
    parent::_construct();
    $this->addData(array(
        'cache_lifetime' => 999999999,
        'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG, Mage_Catalog_Model_Category::CACHE_TAG),
        'cache_key' => md5($this->_data);
    ));

}

Your container class could look something like this to avoid caching entirely if you prefer:

class Namespace_Module_Model_Pagecache_Container
    extends Enterprise_PageCache_Model_Container_Abstract
{
    /**
     * Get container individual cache id
     *
     * Override to return false to cause the block to never get cached
     *
     * @return string
     */
    protected function _getCacheId()
    {
        return false;
    }

    /**
     * Render block content
     *
     * @return string
     */
    protected function _renderBlock()
    {
        $block = $this->_placeholder->getAttribute('block');
        $block = new $block;

        // only needed if the block uses a template
        $block->setTemplate($this->_placeholder->getAttribute('template'));

        return $block->toHtml();
    }

    /**
     * Generate placeholder content before application was initialized and
     * apply to page content if possible
     *
     * Override to enforce calling {@see _renderBlock()}
     *
     * @param string &$content The content
     *
     * @return bool
     */
    public function applyWithoutApp(&$content)
    {
        return false;
    }
}

You may also want to read over Vinai's Answer on the 4 states of the FPC to help understand what each means and how it can relate to your issue of blocks

References and further reading:

Also here is a good flowchart of the FPC workflow I found in a developers paradise slide awhile back that I think helps visually illustrate how the FPC works.

Magento FPC flowchart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.