首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Magento中替Block启用Cache方法

2012-09-28 
Magento中为Block启用Cache方法在Block类的_construct(不是构造方法)方法中加入以下代码: Php代码public f

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

热点排行