I would like to add caching for a block on a per product basis.
I tried just adding:
protected function _construct()
{
//cache for half a day
$this->setCacheLifetime(43200);
}
The problem is a single cache entry is used for every product. So if I load the first page with products that has specific html in it. Then on all other products the same html is shown.
So it is caching the entire block using a default block tag.
How would I cache the block for the page/url as well as per product?
Update:
$this->getProduct()->getId() to the getCacheKeyInfo() function I get the following error:
PHP Fatal error: Call to a member function getId() on null
Update: Add Block Code
class Module_Sticker_Block_Badge extends Mage_Core_Block_Template
{
protected function _construct()
{
//cache the stickers for half a day
$this->setCacheLifetime(43200);
// Add cache tags
// cache tags are used as handles for clearing certain caches
$this->setCacheTag(array(
Mage_Core_Model_Store::CACHE_TAG,
Mage_Cms_Model_Block::CACHE_TAG,
Tengisa_Sticker_Model_Sticker::CACHE_TAG
));
}
// Cache key is unique per bit of HTML
public function getCacheKeyInfo()
{
return array(
Module_Sticker_Model_Sticker::CACHE_TAG,
$this->getNameInLayout(),
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
//Product id
// $this->getParentBlock()->getProduct()->getId()
// Mage::registry('product')->getId()
$this->getProduct()->getId()
);
}