Magento: How to cache product loading

You probably know the famous $product->load($product->getId()) that populates your product object with data retrieved from database. This method does not use native Magento caching while it could be useful to avoid numbers of repetitive MySQL queries. Here is a short example showing you how to cache Magento product loading.

Module configuration

Open file app/code/local/Namespace/Catalog/etc/config.xml:

<?xml version="1.0"?>
                    <label>Product Loading</label>
                    <description>Cache of product attributes loading.</description>

We have created a new cache type that will allow us to easily enable or disable Magento product loading. We now have a new cache type in admin panel:

Override of load() method

Open file app/code/local/Namespace/Catalog/Model/Product.php:

class Namespace_Catalog_Model_Product extends Mage_Catalog_Model_Product
    public function load($id, $field = null)
        if (null !== $field || !Mage::app()->useCache('product_loading')) {
            return parent::load($id, $field);
        // Caching product data
        $storeId = (int) $this->getStoreId();
        $cacheId = "product-$id-$storeId";
        if ($cacheContent = Mage::app()->loadCache($cacheId)) {
            $data = unserialize($cacheContent);
            if (!empty($data)) {
                foreach ($data as $key => &$value){
                    $this->$key = $value;
        } else {
            // You can call some heavy methods here
            try {
                $cacheContent = serialize(get_object_vars($this));
                $tags = array(
                    Mage_Catalog_Model_Product::CACHE_TAG . '_' . $id
                $lifetime = Mage::getStoreConfig('core/cache/lifetime');
                Mage::app()->saveCache($cacheContent, $cacheId, $tags, $lifetime);
            } catch (Exception $e) {
                // Exception = no caching
        return $this;

I’ll let you check for yourself if the number of SQL query decreases well after the cache is enabled but there are some debug bars that can make your life easier, here is a good one:

11 Responses to “Magento: How to cache product loading”

  1. Anh Quy says:

    Hi Reinke.
    great article, but there is a problem, when you turn on the cache. product information in the admin is having problems about display, the default value and default store view use the same cache and product information shown is incorrect.
    can you help me to handle the case this!

  2. Marc says:

    This looks extremely useful. Thanks for sharing this Johann.

    Quick question though (since there’ve been no updates to the comment thread):
    Has the problem reported by Anh Quy been fixed?

  3. Ryan says:

    Hello, great work. Just wondering if you fixed the admin issues?


  4. Mathis says:

    For Fixing Issues with the Admin Panel u have to set “Mage::app()->getStore()->isAdmin()” at the top:
    also added to [email protected]:

    public function load($id, $field = null)
    return parent::load($id, $field = null);

  5. Mido says:

    Maybe a stupid question: but i was trying to add this method of caching products.

    So i created the directories like you ment, but i cant get extra cache type. What i am doing wrong?

    Using Magento 1.7

    looking forward to your reply!

  6. Joshua says:

    I am also working on getting this working, I have a custom extension that I placed this in and it’s not fireing, so I instead put it into it’s own name space and folder as suggested in this article and still can’t get it to fire.

    I have added debugging points in ths script to log data using Mage::log but nothing get’s written to the log, so there is something funny going on not allowing the Load to trigger.

  7. Eric says:

    I would recommend updating the cache tags. With the current setup whenever any product is updated all product cache records are removed. A better solution would seem to be updating the tags entry in the config to:
    And in the load method:

  8. Toni says:

    I am having the same issue as some people here. The folder structure was recreated and the files have been created and uploaded to corresponding folders. However the option does not show in the back and as suggested by the original post.

    What am I doing wrong here?

  9. Dimitry says:

    Thanks for the code! I keep coming up on a problem: I get a “Node no longer exists” error when unserializing. Have you seen this problem?

    • Dimitry says:

      Found a solution. After parent::load($id), add $this->_urlModel = null; It prevents the _urlModel attribute from being cached. That’s okay since it’s always repopulated by Mage_Catalog_Model_Product::getUrlModel().

Leave a Reply

* Required fields