Cache Actions module rules your Drupal cache

Sunday, June 27, 2010 - 12:15

Sometimes you wish you could have more control over you cache in order to invalidate it when it is necessary, and be able to invalidate specific parts of the cache that has been updated. One approach to do that is to use Rules together with Cache Actions.


Caching is great, since it let's you serve your content a lot faster, while preserving your precious CPU cycles. Drupal has caching built in for anonymous users, and the popular modules Views and Panels ships with a time-based cache which let's you cache individual views and panels which can be heavy to serve for every page load even for logged in users.

When you use caching, you can leverage all the features of views and panels which sometimes are just too much to handle if you have a lot of logged in users.

So what is the problem then? Well, when you use a time-based cache, which is the caching mechanisms that ships with both Panels and Views, you don't get much control over when the cache gets invalidated. It could be invalidated too early, meaning that nothing interesting has happened but you have to rebuild the cache anyway, or even worse, you keep serving cached content even if a user has posted new content, for instance a new blog post or a comment. This will make your average user quite confused.

A solution: Rules and Cache Actions

The Rules module let's you create rules that say for instance "When content is created and that content is a blog, then do this action"

A rule consists of a trigger and an action, and can potentially contain a number of conditions that has to be met in order for the rule to be valid.

So, by using the Rules module you can make stuff happen when other stuff happens on the site. That means we could use it to invalidate our cache when it is necessary!

The module Cache Actions adds actions to rules that let's you clear Drupal cache bins, but also the cache of specific Views, Panel pages and Mini panels. By using them together you can create fine-grained rules for clearing the cache when you need to.


You run a very popular blog, and you force your members to create an account on your site in order to comment. You have a lot of eager commentators, and there are many of them logged in at any time on your blog (which means that the drupal anonymous cache isn't used).

You love Views and Panels and you have set up a view that shows all your blog entries. Since you like yourself so much you have set up a panel page for your blog nodes, that includes information about you in every blog post in a column to the right. You also display your comments right below your content.

Your site looks great and you love it. You soon find out however, that your awesome site take up a lot of performance since so many commentators are logged in. You decide to turn on the time-based cache for your view and your panel. This eases the stress on your server and everything seems fine for a while.

One day you start to receive emails from some of your adoring fans. They claim that they have commented on your site, but that the comments don't show up. They also complain about the frequency of your blog posts, they only show up every two days or so. You realize that you have set your time-based cache to two days for both your view and your panel, since the stress on the server was a pain.

This is where Cache Actions and Rules comes in handy. By setting up rules, you can make the cache invalidate itself when necessary. You set up the following rules:

  • Clear the cache of the node panel when a new comment is created.
  • Clear the cache of the node panel and the view when content is updated.
  • Clear the cache of the view when new content is created.
  • Clear the cache of the panel when a comment is deleted.
  • Clear the cache of the view when content is deleted.

Now you can always be sure that your users see the latest and greatest content, while your cache is working as well as it could without disturbing the workflow of the site. Of course, if you have lots and lots of commentators commenting all the time, you will receive a performance hit compared to a cache that would have lived for a longer period of time.

Limitations, and the future

Currently, Cache actions only supports invalidating a whole panel, not individual panes. It doesn't support different contexts currently either, it just invalidates all contexts. This is something that will be included in the 2.x version of Cache Actions, where a true panels cache plug-in will be created.

Currently, you can only clear the cache of one view (or all views if you clear the whole views cache bin) with a rule. It will be possible to clear out more than one view with one rule in 2.x.