Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento how to cache a productCollection

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, and it is down to 2 product collection files I have made to show new products and best selling products.

As soon as i remove these from the home page, the page loads in less than .5 seconds.

So, can anyone help with optimising and caching a productCollection? I have APC installed and running on the server, but Im not sure it is caching the files located in app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml

So, my collection call for best selling (most viewed actually) looks like this;

    <?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)
    ?>

How can I further optimise this?

like image 365
Tom Dollar Avatar asked Mar 14 '13 11:03

Tom Dollar


People also ask

How do I enable cache in Magento?

Enable/Disable Cache Types In your Magento backend, go to System > Tools > Cache Management. Check the box on which cache type you want to enable/disable. Then on the top left corner, select the appropriate action (Enable/Disable) and click the Submit button.

How can be create custom cache in Magento?

Create a new cache type To create a new cache type, you will have to create app/code/Optiweb/MegaMenu/etc/cache. xml in your module. Now you can see your new cache type on the Magento Cache Management page. To use this new cache type, you will need a helper file.

What does Flush Magento cache do?

Flush Magento Cache is a feature to clear cache from the cache storage. It removes items in the default Commerce cache (var/cache). The items are cleared based on their associated Commerce tags. Adobe recommends enabling flush cache actions only for admins.


2 Answers

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/
like image 178
Renon Stewart Avatar answered Sep 28 '22 02:09

Renon Stewart


If you want to cache $collection, there is already a built-in possibility for collection caching in 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') 
        );
    }

Credit for above code: apiworks.net (http://www.apiworks.net/2015/01/magento-collection-caching.html)

like image 33
Darko Goleš Avatar answered Sep 28 '22 00:09

Darko Goleš