6

我注意到我的主页需要很长时间才能加载——根据 site24x7.com,实际上超过 6 秒,所以我一直在关闭元素以尝试确定原因,这取决于我制作的 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)
    ?>

我该如何进一步优化呢?

4

2 回答 2

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

于 2013-03-14T12:52:44.213 回答
4

如果你想缓存 $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') 
        );
    }

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

于 2017-01-11T08:12:41.377 回答