读书人

Magento中替Block启用Cache方法

发布时间: 2012-09-28 00:03:35 作者: rapoo

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());  
?

读书人网 >开源软件

热点推荐