Magento block cache optimizations

We recently ran into an issue with a customer where they emailed us and said were going to be running a promotion on our site and were expecting a lot of traffic. Naturally we were really excited for this and began to talk about all the optimizations we can do to the servers hardware and Magento to prepare their cart to handle all those customers. Only thing at the bottom of the email the client wrote, oh by the way were running the promotion tomorrow. We live for this stuff so we took this as a challenge and began brain storming.

So we regrouped and came up with an efficient solution that only took about 30 minutes to implement and we could achieve fast and efficient speeds with Magento block cache and Redis cache which brought the load times from around 2.3 seconds to 700ms. We used Redis but regular Magento cache works just fine, you can download the Redis client from Colin Mollenhour’ Github Page. So after a few minutes we found the problem pages which were the category pages and the product pages the easiest thing to do is to rewrite the block you need and add the block caching code.

After rewriting the block your going to need to add some details about the data you want to cache. You can download the sample extension here to help you get started using block cache 🙂

In the constructor your going to want to add the cache_liftime key with an expiration in seconds, we set this pretty high since we want it to live in cache as long as possible.

The next key your going to want to set is the cache_tags which is an array of cache tags these are tags that block cache is observing to invalidate the cache. The two tags we used were the product cache tag and the category cache tag altering any product or category would invalidate that block so your not serving stale content to your customers.

The final key is probably the most important the cache_key which is the identifier in the cache. Your going to want to make this unique especially for the category view block but if your caching a block that it isn’t different between pages or users you can use a static key.