3

I rewrite catalog product list block for caching issue. Please review code and give your advices to improve caching and cache-validating issues for huge product listing category pages. Who has better configuration for caching blocks like this?

class Ssd_Fix_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
{
    const CACHE_TAG = 'CATEGORY_PRODUCTS';

    public function getCacheKey()
    {
        $url = md5(Mage::app()->getRequest()->getOriginalRequest()->getRequestUri());
        if (!$this->_cache_key) {
            $key              = "CATEGORY_PRODUCTS_" . $url;
            $this->_cache_key = $key;
        }
        return $this->_cache_key;
    }

    public function getCacheTags()
    {
        $data = array(self::CACHE_TAG);
        if ($category = Mage::registry('current_category')) {
            $data[] = Mage_Catalog_Model_Category::CACHE_TAG . "_" . $category->getId();
        }
        if (count($products = $this->getProductList())) {
            foreach ($products as $p) {
                $data[] = Mage_Catalog_Model_Product::CACHE_TAG . "_" . $p->getId();
            }
        }
        return $data;
    }

    public function getCacheLifetime()
    {
        return 60 * 60 * 24;
    }

    //other methods here
}

2 Answers 2

8

It would be better to override the getCacheKeyInfo() method instead of getCacheKey().

public function getCacheKeyInfo()
{
    $info = parent::getCacheKeyInfo();
    // Add any additional data you want
    $info[] = Mage::app()->getRequest()->getOriginalRequest()->getRequestUri();
    return $info;
}

The hashing of the value is taken care of automatically.
This will take care important data like the current store view, the theme and the used template are included.

Generally the request URI is not considered ideal for cache key purposes, since there are many factors that could influence the blocks output that are not contained in the URI.

For example, if the block output contains prices, be sure to include the customer group id (linked to the customer's tax_class_id) in the cache key info array.

There can also be several permutations of the request URI that all refer to the same page, for example with a trailing / and without. It generally leads to better results to build the cache key array manually.

For the cache tags, be sure to include the cache tags of all displayed products, too (already done in 1.8/1.13).

I'd also recommend using a full page cache in addition to tuning block caching.

3
  • getCacheKeyInfo is + for me, thanks, but I can use FPC for Magento CE?
    – mageUz
    Jun 14, 2013 at 14:01
  • There are many full page cache solutions available for CE. Some are free, some are commercial, some are Magento only, some utilize a reverse proxy like varnish.
    – Vinai
    Jun 14, 2013 at 14:08
  • Is it enough to override the core Mage_Catalog_Block_Product_List in order to enable caching the product list?
    – Dor
    Jan 31, 2014 at 10:24
1

huge product listing category pages

Maybe a deterministic algorithm like Monte Carlo is a good starting point. For example you can cache with a probability of (10% * isThisHardToDoFactor) . After showing the product 10 times with a normal isThisHardToDoFactor, you have a high chance that it will try to cache. If it's only showed once or twice (while testing), it has a low chance to fill up your cache. Also make the TTL a bit random, you don't want a lot of cache entries expires at exactly the same time.

https://en.wikipedia.org/wiki/Monte_Carlo_algorithm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.