Magento如何缓存productCollection

Tom*_*lar 6 caching apc magento

我注意到我的主页需要花费很长时间才能加载 - 超过6秒insite24x7.com,所以我一直在关闭元素以尝试确定原因是什么,这是我做的2个产品收集文件展示新产品和畅销产品.

一旦我从主页中删除这些,页面加载时间不到0.5秒.

那么,任何人都可以帮助优化和缓存productCollection吗?我在服务器上安装并运行了APC,但我不确定它是否缓存位于app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml中的文件

所以,我的畅销书(实际上看得最多)看起来像这样;

    <?php $storeId = Mage::app()->getStore()->getId(); // return current store id  ?>
    <?php $_productCollection= Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addStoreFilter($storeId)
    ->addViewsCount()
    ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
    $_productCollection->getSelect()->limit(8)
    ?>
Run Code Online (Sandbox Code Playgroud)

我该如何进一步优化这个?

Ren*_*art 7

尝试

  $storeId = Mage::app()->getStore()->getId(); 
  $cache = Mage::getSingleton('core/cache');
  $key = 'homepage-most-view-' . $storeId;

  if(! $data = $cache->load($key)){
      $_productCollection= Mage::getResourceModel('reports/product_collection')
      ->addAttributeToSelect('*')
      ->addStoreFilter($storeId)
      ->addViewsCount()
      ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
      ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
      $_productCollection->getSelect()->limit(8)
      // get the element you need from  $_productCollection and store in $array
      $data = serialize($array);
      $cache->save(urlencode($data), $key, array("homepage_cache"), 60*60*24);
  }
  else{
      $data = unserialize(urldecode($data)); 
 }
Run Code Online (Sandbox Code Playgroud)

看到


小智 5

如果你想缓存 $collection,Magento 中已经有内置的集合缓存功能。

 $_productCollection= Mage::getResourceModel('reports/product_collection');

$cache = Mage::app()->getCache(); //Let's get cache instance
        $cache->setLifetime(86400); //Here we set collection cache lifetime
        $_productCollection->initCache(
            $cache,
            'Bestsellers_', //this is just custom prefix
            array('collections') 
        );
    }
Run Code Online (Sandbox Code Playgroud)

上述代码的来源:apiworks.net ( http://www.apiworks.net/2015/01/magento-collection-caching.html )