首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Magento如何缓存productCollection

Magento如何缓存productCollection
EN

Stack Overflow用户
提问于 2013-03-14 19:30:18
回答 2查看 10.7K关注 0票数 6

我注意到我的主页花了很长时间来加载--实际上根据site24x7.com,超过6秒,所以我一直在关闭元素,试图确定原因是什么,这是我制作的两个产品集合文件,以显示新产品和畅销产品。

一旦我从主页中删除这些内容,页面就会在不到.5秒的时间内加载。

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

所以,我的收藏号召畅销(实际上观看次数最多)是这样的;

代码语言:javascript
运行
复制
    <?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)
    ?>

我怎样才能进一步优化它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-14 20:52:44

试一试

代码语言:javascript
运行
复制
  $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)); 
 }

看见

  • http://www.nicksays.co.uk/developers-guide-magento-cache/
  • http://inchoo.net/ecommerce/magento/magento-block-caching/
票数 7
EN

Stack Overflow用户

发布于 2017-01-11 16:12:41

如果你想缓存$collection,Magento已经内置了集合缓存的可能性。

代码语言:javascript
运行
复制
 $_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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15408003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档