Main navigation

How to Fix Caching for Views With Exposed Filters in Drupal 7

One of the better features of the views module in Drupal is the ability to cache your view's output. This can come in handy when your view is doing a lot of computation. Caching your view will save your server a lot of unneeded work. One of the big current drawbacks of this feature is if you enable caching for your view and you have an exposed filter, you'll run into the following scenario:

User A enters a value for the filter

Then User B enters a different value for that same filter

Yikes! As you can see, caching is serving up User A's result set for User B! User B just wanted to see results in North Carolina and recieved results in Oregon! This is not very helpful as-is. Thankfully, there is a handy patch to views available to you at: http://drupal.org/node/1055616. Let me go ahead and explain how to apply this patch so you can have nice cached views with exposed filters in Drupal 7.

UPDATE: Since I wrote this article, there has since been another patch released. Feel free to try and use later patches (#81 and higher) instead of #71. If you do this, be sure to fully test views' caching to the fullest you can before deploying this fix anywhere.

Fire up a terminal and navigate to your drupal directory: cd ~/Sites/my_drupal_site/drupal

If you have a views with an exposed filter but without any preloaded default content, is there any point to use the Views' caching feature? U only use it to load the default results, right? Or does it also speed up dynamic results in some way?

This will apply caching to the results, even on non-default results. It just caches the results per each value used in the exposed filter. For a generic search form this might not make a lot of sense. However, for exposed filters where many users might be entering the same exact values (say just the postal code for a store locator), this can help increase performance.

Thanks, I've been trying to find out why my date span filters were working fine in preview mode but completely useless in browser tests though I'd disabled views caching and drupal caching for development. The latest patch saved me - thanks much for pointing it out here!