缓存自定义块

时间:2018-01-24 10:51:50

标签: magento magento-1.9

我试图从我的模块中缓存一个自定义块但是不起作用,我的代码有问题吗?

 $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); 
        }

1 个答案:

答案 0 :(得分:0)

您的代码有点混乱序列化:)

看看这个

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

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

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