Magento Category Caching

The website I administer has lots of configurable products, with many colour/size variations.  The category page load speed had been very slow, and I’d been looking to work on the caching for some time.  I put block caching in using the tutorial here on the product list and it’s made a remarkable difference, with categories loading in 1-2 seconds from a time of 10-12 seconds (worse categories).  I added the block cache to this code:

/app/code/core/Mage/Catalog/Block/Product/List.php

protected function _construct() {
  $this->addData(array('cache_lifetime' => 99999999, ));
}

public function getCacheTags()
{
  return array(Mage_Catalog_Model_Product::CACHE_TAG);
}

public function getCacheKey()
{
  $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
  return $this->getRequest()->getRequestUri();
}

I hope it helps others to speed up their sites too – ps you probably shouldn’t follow my bad example and update the core file, but the one in the local directory.

One Reply to “Magento Category Caching”

  1. I needed to get this to work for products where the VAT is calculated based on delivery address. Therefore for prices outside the EU it is essentially ex-VAT that is needed. Equally, for EU countries it has to include the VAT and cached ex-VAT prices were no good.

    I also changed the cache key to include the customer group. Therefore, a customer group with a staff or trade discount does not pollute the cache with their preferential price.

    In multi-store the below works if you have the store code in the URL, e.g. ‘store.com/us’, ‘store.com/uk’ etc.

    class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
    {

    protected function _construct() {
    $this->addData(array( ‘cache_lifetime’ => 2419200,
    ));
    }

    public function getCacheTags() {
    return array(Mage_Catalog_Model_Product::CACHE_TAG);
    }

    public function getCacheKey() {
    $store=Mage::App()->getStore()->getCode();
    $country=Mage::getModel(‘tax/calculation’)->getRateRequest()->getCountryId();
    $custgroup=Mage::getSingleton(‘customer/session’)->getCustomer()->getGroupId();
    return $this->getRequest()->getRequestUri().”_”.$store.”_”.$country.”_”.$custgroup;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *