`

Magento中为Block启用Cache方法

阅读更多

在Block类的_construct(不是构造方法)方法中加入以下代码: 
Php代码
public function _construct()   
        {   
        $this->addData(   
            array(   
                'cache_lifetime'    => 3600,   
                'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG),   
                'cache_key'         =>  'productfaq_'.Mage::registry('product')->getId().'_'.Mage::app()->getStore()->getId()   
                . '_' . Mage::getDesign()->getPackageName()   
                . '_' . Mage::getDesign()->getTheme('template')   
                . '_' . Mage::getSingleton('customer/session')->getCustomerGroupId()   
            )   
        );   
           
        parent::_construct();   
        }  

cache_key必须唯一。 
在获得数据的方法中加入: 
Php代码
$faq_data = @unserialize( Mage::app()->loadCache($this->getCacheKey()) );   
//从数据库取出数据并存入到faq_data 中   
    if (!$faq_data) {   
               
            $faq_data = array();   
                        ...   
                        $faq_data[]=array('customer_id' => $customerId,   
                    'name' => $sName,   
                    'avatar' => $sAvatar,   
                    'question' => $sQuestion,   
                    'answer' => $sAnswer,   
                );   
       
       }   
Mage::app()->saveCache(serialize($faq_data), $this->getCacheKey(), $this->getCacheTags(),$this->getCacheLifetime());  
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics