Updating the page cache on time

On RVParking.com, we have a detailed directory listing page. It is complex CCK node themed, with an included Google map for location. Also included are two embedded Views. One for photos (a slideshow type photo with AJAX paging) and a View of Reviews (a review node type, with fivestar voting and attached pictures among other things. Both use node references to attach them to the park.

Our Problem: Cached pages with embedded views show stale view data.

The crux on the problem is that a park page would not be rebuilt when a new review or photo was posted. At first I thought it was the view that wasn’t being updated the new photos or reviews. After even turning off the view cache, the problem persisted.

So I looked higher up the cache ladder, and figured out it was the node cache that was the issue. Of course, how would the park node cache know to expire its cache when a review or photo with a reference to this park had been added or updated. But we can tell it to using the nodeapi_hook().

function rvparks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
     case 'insert':
     case 'update':
       if ($node->type == 'review' OR $node->type == 'park_photo') {
       $url = url('node/'. $node->field_park_reference[0]['nid'], array('absolute' => TRUE));
       // delete cache entries for that url
       cache_clear_all($url, 'cache_page');
     }
   }
}

This code fragment executes during either the insert (node create) or update (node edit) actions. It checks to see if either a photo or review node has been created. If so, then it form the full URL for the node that is in the node reference field. The Drupal caches uses the full URL to create the cache references, thus the ‘absolute’ => TRUE is needed.

The cache_clear_all() does all the work to delete the cache entry.

Now, when the RV park page is viewed after a new review is posted, the whole node cache is recreated. Problem solved.

The next step in making our site faster will be to add Boost. I am pretty sure this method will still work under Boost. About to find out.