Drupal 6 Performance Optimization Using Views and Panels Caching

TJ Holowaychuk

February 2010

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,
AS node_data_field_photo_gallery_photo_field_photo_gallery_photo_fid,
AS node_data_field_photo_gallery_photo_field_photo_gallery_photo_list,
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

Clearing your Views 2 module cache

If you are working with the Views module in your site, you'll want to know how to clear your Views module cache so that updates and tweaks you make to your Views via the Views 2 UI show up immediately. The Views module will cache existing View configuration, settings, and content, so if you're not seeing the changes you've made or are experiencing other issues with your Views, you'll want to make sure to clear the cache.

Here are the steps to clear your Views cache:

  1. Go to Site building | Views to access your Views admin.
  2. At the top of the page you'll see a Tools button. Click on it.
  3. The simplest method of clearing your Views cache is to click on the Clear Views' cache button.
  4. You will receive a message: The cache has been cleared.
Drupal 6 Performance Optimization Using Views and Panels Caching

There are other settings available to you through this Views Tools page. You can choose to Add View signatures to all SQL queries—doing this will add a VIEWS string to the WHERE clause in your query. This can cause a performance issue potentially though, as it is more work for the database. So, you can opt out of this. Drupal advises that you should only use this method for troubleshooting and development purposes.

You can also choose to disable Views caching. However, we do want Views to cache data because it will give us a performance enhancement. We can easily clear our Views cache using the above method if necessary, so let's leave the caching functionality for Views enabled.

If you do not have the Advanced Help module installed or if it's disabled, you can tell Views to ignore this and not show you errors telling you the Advanced Help integration with Views is disabled. Again, this is optional, but a nice setting to have if you do not want to see the error messages popping up.

We can also enable Views performance statistics if we have the Devel module installed. Go ahead and make sure this is checked. It should be, if you have been receiving Devel module Views statements regarding build times as noted earlier.

Set the Page region to output performance statistics as the Footer region—this is the default.

Drupal 6 Performance Optimization Using Views and Panels Caching

Click on the Save configuration button.

Using Panels caching

The Panels 3.x module allows you to distribute your site's content and Views into custom layouts. You can use Panels to set up and configure these layouts and then integrate content into the layout that you choose. The Panels module uses what the module developers call a pluggable caching mechanism. This mechanism allows you to set up caching for all of your panels as a whole or for specific individual content panes within one panel. This allows for a large amount of caching flexibility. This section assumes that you have some experience using the Panels module to configure a panel layout and add content to it. I'm going to walk you through those configuration steps quickly and then we'll look at Panels caching more closely.

Creating a panel and adding content to it

To get started with Panels caching, make sure you have a Panel to use on your site. I'm going to go ahead and set up a Panel quickly and add some content to it, which we'll use to test the Panels caching mechanism.

To create a new panel go to Site building | Panels and click on the Panel page link to create a new panel. Give the panel an administrative title, machine name, and a path. Click on the Continue button.

Choose a layout for your panel. Let's use the Two column bricks layout. Click on the Continue button.

This will launch your layout admin page where you can click on each panel section to add content to it. Add content to your panel's regions. We'll add two of our Views to the panel. Let's add our photo gallery View to our Top panel region. Click on the gear icon in the left corner of the region to open the region options. Click on Add content. Browse for your photo gallery view and add it to that panel region.

Once added, you will notice another gear icon in the upper right corner of the content pane you just added to your panel region. Click on this icon. When you click on this icon, a selection box will open and you will see a link to Cache method. The cache method for this content in your panel is currently set to None. Click on this link to open the cache method settings.

Drupal 6 Performance Optimization Using Views and Panels Caching

Similar to how you were presented with the Views caching configuration, Panels caching will ask you if you want to implement No caching on the content in the Panel or Simple cache. Select the Simple cache radio button and click on Next.

Drupal 6 Performance Optimization Using Views and Panels Caching

Like the Views caching, panels simple cache is time-based. Content will remain cached for the length of the time limit that you configure. Set a Lifetime cache time limit. We'll set a Lifetime cache of 1 hour.

Also, set a Granularity cache if you are using contexts or arguments in your Panel. We're not using either, so we'll leave this set to None. If you do have contexts selected in your Panel, you can further cache depending on the context using this granularity cache mechanism. Again, Drupal and panels are giving you a lot of flexibility and granularity here.

Drupal 6 Performance Optimization Using Views and Panels Caching

Click on the Save button to save your cache settings. Make sure to update and save your Panel again now that you have configured its cache mechanism by clicking on the Update button. Your panels are now set to cache the content you specifically configured using the cache configuration above.


Here, we learned the following performance tips:

  • Configured caching for our Views and learned how to enable and disable caching per View. We also looked at how the Devel module returns information about our Views build times based on these cache settings.
  • Learned how to clear our Views cache.
  • Looked at how the Panels module uses caching and how you can enable caching per panel.

If you have read this article you may be interested to view :

You've been reading an excerpt of:

Drupal 6 Performance Tips

Explore Title