缓存自定义块



>im 尝试从我的模块缓存自定义块但不起作用,我的代码有问题吗?

 $cacheId = 'my_cache_id';
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
    $data = unserialize($data);
    return $data;
} else {
       $collection = Mage::getModel('catalog/product')->getCollection()
                            ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
                            ->addMinimalPrice()
                            ->addTaxPercents()
                            ->addStoreFilter()
                            ->addAttributeToSort('entity_id', 'desc');
        // CategoryFilter
        $collection = $this->categoryFilter($collection);
        // getNumProduct
       $collection->setPageSize($this->getWidgetCfg('limit'));
        Mage::app()->getCache()->save(serialize($collection), $cacheId);
        return Mage::app()->getCache()->load($cacheId); 
        }

你的代码有点乱

,序列化:)

看看这个

    // getNumProduct
   $collection->setPageSize($this->getWidgetCfg('limit'));
    Mage::app()->getCache()->save(serialize($collection), $cacheId);
    return Mage::app()->getCache()->load($cacheId);

您正在从缓存返回序列化对象。

只需返回$collection,这甚至可以提高初始性能

最新更新