Magento how to cache a productCollection

前端 未结 2 624
无人共我
无人共我 2021-02-03 14:44

Ive noticed my home page is taking a long time to load - over 6 seconds infact according site24x7.com, so ive been switching elements off to try and determine what is the cause,

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-03 15:41

    Try

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

    See

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

提交回复
热议问题