给Magento常用的Block添加缓存

1、产品页面

修改文件:app\code\local\Mage\Catalog\Block\Product\View.php
在_prepareLayout方法前面添加方法:
 protected function _construct()
 {
     $this->addData(array(
        'cache_lifetime' => 86400, 
        'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()),
        'cache_key' => $this->getProduct()->getId(),
     ));
 }
这里使用catalog_product_productid使用cache_tags

2、分类页面
修改文件:app\code\local\Mage\Catalog\Block\Category\View.php
在_prepareLayout方法前面添加方法:
protected function _construct()
{
        $this->addData(array(
              'cache_lifetime' => 86400,
              'cache_tags' => array($this->getCacheKeyInfo()),
              'cache_key' => $this->getCacheKeyInfo(), 
        ));
}
public function getCacheKeyInfo(){
        $paging = '';
        $limit = '';
        $mode = '';
        $order = '';
        $productListBlock = $this->getChild('product_list');
        if ($productListBlock){
               $toolbarBlock = $productListBlock->getChild('product_list_toolbar');
               if($toolbarBlock){
                     $paging = $toolbarBlock->getCurrentPage();
                     $limit = $toolbarBlock->getLimit();
                     $mode = $toolbarBlock->getCurrentMode();
                     $order = $toolbarBlock->getCurrentOrder();
               }
        }
        return array(
               'CATEGORY_VIEW',
               Mage::app()->getStore()->getId(),
               Mage::getDesign()->getPackageName(),
               Mage::getDesign()->getTheme('template'),
               $this->getCurrentCategory()->getId(),
               'template' => $this->getTemplate(),
               $paging,
               $limit,
               $mode,
               $order
        );
}
3、cms/page页面
修改文件:app\code\local\Mage\Cms\Block\Page.php
在_prepareLayout方法前面添加方法:
const CACHE_KEY = 'cms_page'; 
protected function _construct()
{
    $this->addData(array(
        'cache_lifetime' => 86400,
        'cache_tags' => array('cms-'.$this->getPage()->getIdentifier()),
        'cache_key' => $this->getCacheKey(), 
    ));
 }
public function getCacheKey() 
{
        $storeId = $this->getStoreId();
        $currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
        $uri = $this->getRequest()->getRequestUri() . self::CACHE_KEY;
        return $storeId.$uri.$currencyCode;
}
这里使用cms-pageidentifier作为cache_tags



你可能感兴趣的:(Magento)