Magento – Magento 1.9.2.0 static block display issues

block-cachece-1.9.2.0magento-1.9static-block

I have a website with multiple static blocks which was working in 1.9.1.0, but with 1.9.2.0 the static blocks start displaying sporadically, as they sometimes show the wrong block rather than the correct block. Sometimes they display as desired. Does anyone know how to solve this issue which may be related to this issue?

Best Answer

I had this problem with EE 1.14.2 and it looks like the same issue has come up in CE 1.9.2. I documented my problem and solution on this SE question.

Basically due to the following code being added to the constructor of Mage_Cms_Block_Block:

$this->setCacheTags(array(Mage_Cms_Model_Block::CACHE_TAG));
$this->setCacheLifetime(false);

CMS static blocks are now cached. The problem arises from how the cache key info is generated. It falls back to the Mage_Core_Block_Abstract behavior of using the blocks name in layout. If the block hasn't been added with layout, e.g on a cms page, this name doesn't exist. This can result in static blocks sharing the same cache key and getting mixed up in cache.

My solution was to override the Mage_Cms_Block_Block class and set the cache key info based on the block id and current store.

/**
 * Override cms/block to add cache key. This started being a problem as of EE 1.14.2 and CE 1.9.2 when the _construct
 * method was added which turns on caching for cms blocks
 */
class Mysite_Cms_Block_Block extends Mage_Cms_Block_Block
{

    /**
     * If this block has a block id, use that as the cache key.
     *
     * @return array
     */
    public function getCacheKeyInfo()
    {
        if ($this->getBlockId()) {
            return array(
                Mage_Cms_Model_Block::CACHE_TAG,
                Mage::app()->getStore()->getId(),
                $this->getBlockId(),
                (int) Mage::app()->getStore()->isCurrentlySecure()
            );
        } else {
            return parent::getCacheKeyInfo();
        }
    }
}

Obviously this would need to be added in your own module with a config.xml file and block override etc. Alternatively you could copy Mage_Cms_Block_Block to the local code pool and add the cache key there.

You can see the new lines added in 1.9.2 here

Related Topic