0

I enable cache for my custom block by adding cache_lifetime in __construct and adding getCacheKeyInfo() function.

cache_lifetime and cach key were added successfully, I verified by going to _loadCache function in Mage_Core_Block_Abstract and dump $this->getCacheLifetime() (shows 86400), dump $cacheKey (shows 4bdf58570c6cc1c082c0a2309d0acbee3f318d90)

But when I dump $cacheData = $this->_getApp()->loadCache($cacheKey);, it always return false.

Why does it return false? I haven't ever modified any core Mage files.

By the way, I'm using apc for backend cache, and memcache for session cache. CE 1.8.1. And of course, I've enabled cache in admin panel already.

UPDATE:

Dump $this->getCacheKey(); in _loadCache(): cb2336b7ccd507da70b5167dcb7e87856284647d

Dumo $this->getCacheKey(); in _saveCache(): 00db354e5f03e1bf0b06da33c491eb2e313d29cc

OMG. Where make it change?

1

Finally I found out why.

This is my getCacheKeyInfo function.

public function getCacheKeyInfo()
    {
        return array(
            'MY_BLOCK_NAME',
            Mage::app()->getStore()->getId(),
            Mage::getDesign()->getPackageName(),
            Mage::getDesign()->getTheme('template'),
            Mage::getSingleton('customer/session')->getCustomerGroupId(),
            'template' => $this->getTemplate(),
            Mage::app()->getStore()->isCurrentlySecure()
        );
    }

When it calls getCacheKeyInfo() the first time in _loadCache(), I don't know why template is not initiated, so $this->getTemplate() return null.

At the second time of calling in _saveCache(), ( _saveCache() is called after >_beforeToHtml(); and _toHtml();) then template is initiated, so $this->getTemplate() return path of template file.

I try to add template data in constructor of my block class ($this->addData(array('template' => $this->getTemplate()));), but it still doesn't work.

So, after all, I delete template element in cache key array.

public function getCacheKeyInfo()
    {
        return array(
            'MY_BLOCK_NAME',
            Mage::app()->getStore()->getId(),
            Mage::getDesign()->getPackageName(),
            Mage::getDesign()->getTheme('template'),
            Mage::getSingleton('customer/session')->getCustomerGroupId(),
            Mage::app()->getStore()->isCurrentlySecure()
        );
    }

It works now!

UPDATED:

OMG! I found out why template name is not initiated at the first time.

Because: My module set template in _toHtml() function, not in constructor (by adding template="path/to/template/file.phml")

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.