Reputation:
Ok, so I have recently been looking at block caching in magento. Looks like a really great way to speed up rendering of pages, but it also looks like a bit of a pain in the arse.
For example, caching category blocks seems to require a lot of messing around to get done properly. Also, there a lot of core blocks which do not utilize block caching that probably could. In these cases, does a developer have to create a single module and then rewrite all of these core blocks, just so as to enable block caching on them?
Are there any other tips with regards to block caching for those new to this subject?
Upvotes: 1
Views: 3638
Reputation: 2088
Block caching can be done in PHP if you prefer. The cache management has to be written in the constructor of the Block
https://magento2.atlassian.net/wiki/display/m1wiki/How+to+use+HTML+output+cache+in+Magento+1.x
protected function _construct() {
$this->addData(array(
'cache_lifetime' => 3600,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
'cache_key' => $this->getProduct()->getId(),
));
}
Upvotes: 0
Reputation: 6965
Block caching does not (in most cases) need to be implemented at the PHP level, it can be done in XML. To enable caching for a block that already exists you'll need to open up a <reference />
to the block, and then set the appropriate data with <action />
calls, like so:
<reference name="myblock">
<action method="setCacheLifetime"><seconds>1800</seconds></action>
<action method="setCacheKey"><key>my-unique-cache-key</key></action>
</reference>
Upvotes: 7