[Drupal] How to set time based caching for blocks in Drupal

| | 2 min read

You might think what is the big deal if your Drupal site performs badly.To put it simply, if the performance of your site is poor,you will lose a lot of potential customers. So, one of the core aspects of a Drupal website is its performance. We had worked on a Drupal site that had several blocks. To enhance the site's performance, one of the steps we had taken was to set up a time base caching for all blocks. Are you curious to know how we did this? Read ahead.

The Drupal Block cache alter module helps to alter block caching. One important thing is that we can set time for cache. This is significant because caching time of blocks should depend on the context. Also, if a block needs to be refreshed while some contents are created, or if we need to cache blocks based on different situations, this module will be useful for you.

Steps for installing and enabling block cache alter module in drupal 6

However,simply enabling the module didnt give the options such as cache time. For this, we had to apply one of the core patches available with the module. To apply patch, copy the suitable patch file to the modules/block folder and run the command,

 patch -p0 < filename 

Then, go to block cache alter settings in admin/settings/blockcache_alter and enable check box core patch. image1.png. This will make more options in block caching, like cache lifetime and refreshing options. image2.png

If you are placing a block using module_invoke(), setting cache doesn't take effect, because block doesn't belong to any region. In this scenario, first you have to set the cache for the block, including cache life time and call the block using module_invoke() as follows.


<?php
//This block is created by views.
//So, in usual cases, module_invoke code looks like, $block = module_invoke('views', 'block', 'view', 'block_delta');
//But here, it goes like this.
$block = module_invoke('blockcache_alter', 'block', 'view', 'views,block_delta');
print $block['content'];
?>

So simple. Hope this will help

Reference:
http://drupal.org/project/blockcache_alter