5 min read

Views caching

The Views 2 module allows you to cache your Views data and content. You can cache Views data per View. We’re going to enable caching on one of our existing Views, and also create a brand new View and set caching for that as well using the test content we just generated. This will show you a nice integration of the Devel functionality with the Views module and then how caching works with Views.

Go to your Site building | Views configuration page and you’ll see many of your default and custom views listed. We have a view on this site for our main photo gallery. The view is named photo_gallery in our View listing. Go ahead and click on one of your Views edit links to get into edit mode for a View.

In our Views 2 interface mode, we’ll see our tabs for default, Page, and/or Block View display. I’m going to click on my Page tab to see my View’s page settings. Under my Basic settings configuration, I’ll see a link for Caching. Currently, our Caching link states None, meaning that no caching has been configured for this view.

Drupal 6 Performance Optimization Using Views and Panels Caching

Click on the None link. Select the Time-based radio button. This will enable Time-based caching for our View page.

Drupal 6 Performance Optimization Using Views and Panels Caching

Click the Update default display button. The next caching options configuration screen will ask you to set the amount of time for both, your View Query results and for your View Rendered output. Query results refer to the amount of time raw queries should be cached. Rendered output is the amount of time the View HTML output should be cached. So basically, you can cache both your data and your frontend HTML output.

Set them both to the default of 1 hour. You can also set one to a specific time and the other to None. Go ahead and tweak these settings to your own requirements. I’m leaving both set to the default of 1 hour.

Drupal 6 Performance Optimization Using Views and Panels Caching

Click on the Update button to save your caching options settings.

You are now caching your View. Save your View by clicking on the Save button. The next time you look at your View interface you should see the caching time notation listed under your Basic settings. It will say 1 hour/1 hour for this setting.

Once you enable Views caching, if you make a change to your View settings and configuration, the results and output of the View may not update while you have caching enabled. So, while in Views development you may want to disable caching and set it to None. Otherwise, this next section will show you how to disable your Views cache while you are in development.

To see the performance results of this, you can use the Devel module’s functionality again. When you load your View after you enable caching, you should see a decrease in the amount of ms (milliseconds) needed to build your Views plugin, data, and handlers. So, if your Views plugin build loaded in 27.1 ms before you enabled caching, you may notice that it changes to something less—for example, in my case it now shows that it loads in 2.8 ms. You can immediately see a slight performance increase with your View build.

Drupal 6 Performance Optimization Using Views and Panels Caching

Let’s go ahead and build a brand new View using the test content that we generated with the Devel module and then enable caching for this View as well.

Go to your Views admin and follow these steps:

  1. Add a new View.
  2. Name the View, add a description and a tag if applicable.
  3. Click on Next.
  4. I’m going to create a View that filters my blog entries and lists the new blog entries in post date order using the Devel content I generated.
  5. Add a Page display to your new View.
  6. Name the page View.
  7. Give the page View a title.
  8. Give your View an HTML list style.
  9. Set the View to display 5 posts and to use a full pager.
  10. Set your caching to Time-based (following instructions above in the first view we edited).
  11. Give the view a path.
  12. Add a Node: Title field and set the field to be linked to its node.
  13. Add a filter in order to filter by Node:Type and then select Blog entry.
  14. Set your Sort criteria to sort by Node:Post date in ascending order by hour.
  15. Your settings should look similar to this:
Drupal 6 Performance Optimization Using Views and Panels Caching

Save your View by clicking on the Save button. Your new View will be visible at the Page path you gave it and it will also be caching the content and data it presents. Again, if you refresh your View page each time you should notice that the plugins, data, and handlers build times decrease or stay very similar and consistent in load times. You should also notice that the Devel database queries status is telling you that it’s using the cached results and cached output for the View build times and the MySQL statements. You should see the following code sitting below your page content on the View page you are looking at. It will resemble this:

Views plugins build time: 23.509979248 ms
Views data build time: 55.7069778442 ms
Views handlers build time: 1.95503234863 ms
SELECT node.nid AS nid,
node_data_field_photo_gallery_photo.field_photo_gallery_photo_fid
AS node_data_field_photo_gallery_photo_field_photo_gallery_photo_fid,
node_data_field_photo_gallery_photo.field_photo_gallery_photo_list
AS node_data_field_photo_gallery_photo_field_photo_gallery_photo_list,
node_data_field_photo_gallery_photo.field_photo_gallery_photo_data
AS node_data_field_photo_gallery_photo_field_photo_gallery_photo_data,
node.type AS node_type,
node.vid AS node_vid,
node.title AS node_title,
node.created AS node_created
FROM {node} node
LEFT JOIN {content_type_photo} node_data_field_photo_gallery_photo ON
node.vid = node_data_field_photo_gallery_photo.vid
WHERE (node.status <> 0) AND (node.type in ('%s'))
ORDER BY node_created ASC
Used cached results
Used cached output

LEAVE A REPLY

Please enter your comment!
Please enter your name here