Creating a rules action to expire cached content.

arcaic - October 15, 2009 - 16:11
Project:Boost
Version:6.x-1.12
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I have a view that is mainly viewed by anonymous users. The content the view is dependant on is only update periodically by myself.

I had been playing around with boost so that I could cache the view served to anonymous users but I didn't want to wait for the cached view to expire when I changed the underlying content, I wanted it to update immediately.

I came up with the following solution that seems to work well.

Assuming you are using the rules module... if not then you will need to download and install it first.

Create a file called boost.rules.inc in the modules/boost directory and place the following code in it (including php tags)

<?php
/**
* Implementation of hook_rules_action_info().
* @ingroup rules
*/
function boost_rules_action_info() {
  return array(
   
'rules_action_clear_page' => array(
     
'label' => t('Clear a page from the boost cache.'),
     
'arguments' => array(
       
'page' => array('type' => 'string', 'label' => t('URL of page to clear')),
      ),
     
'module' => 'Boost',
    ),
  );
}

/**
* Clears a page from the Boost cache
*/
function rules_action_clear_page($page, $settings) {
  if (
boost_is_cached($path)) {
   
boost_cache_expire_derivative($page);
   
watchdog('boost', 'Flushed ' . $page . ' from static page cache.'); 
  }
}

/**
* Action clear page from cache configuration form.
*/
function rules_action_clear_page_form($settings, &$form) {

 
$settings += array('page' => '');

 
$form['settings']['page'] = array(
   
'#type' => 'textarea',
   
'#title' => t('Page URL'),
   
'#default_value' => $settings['page'],
   
'#description' => t('The URL of the page to clear from the Boost cache.'),
  );
}
?>

You will now have a new rules action called 'Clear a page from the boost cache.'

The action takes one parameter which is the path of the content you wish to expire.

On my site I have setup rules to execute the new action whenever the underlying content type has a node added or deleted.

The code seems to work for me and I havent seen any side effects yet but I do not have any real knowledge of boost so would appreciate it if someone who knows could confirm that using 'boost_cache_expire_derivative' like this is ok.

Andy

#1

dbeall - October 15, 2009 - 20:38

watching this thread.. (have not tried it yet), sounds neat!

#2

mikeytown2 - October 15, 2009 - 21:02

Usage of the underlying functions seems to be correct, but... Flushing of views when a node is updated is now built into boost.
Enable these 2 settings under Boost advanced settings
* Clear all cached views pages associated with a node on update/delete
* Clear all cached views pages associated with a node on insert
Nice thing about this is it's all automatic, never have to worry about it again; never have to set anything. Remove the boost.rules.inc file and let me know if these 2 settings do what your looking for.

Having a boost.rules.inc file does seem interesting; could host some of the other tricks in there...

#3

arcaic - October 16, 2009 - 10:03

Hi, Thanks for the prompt reply.

It's not that the existing 'clear all' options don't work its just that they seemed too broad for me. I have several other views whose underlying content changes quite rapidly yet it's acceptable for those views to refresh for anon user less frequently.

If I enable the options mentioned then its on for all views and a lot more processing will be required. The action method seems to be a better way to target very specific cache update requirements. It also seemed like a good idea because it's not invasive at all as none of the actual boost code is modified in anyway.

I think there is a lot of scope for addressing some of the cache expiration/update logic issues that people raise through the use of actions and rules rather then trying for ever more 'clever' cache expiration logic which will never please everyone all of the time.

oh... and thanks for all your work on Boost. It's made a huge difference to the responsiveness of my site.

Andy

#4

mikeytown2 - October 16, 2009 - 10:13

Interesting use case. Adding a checkbox to the "Page configuration Block" stating something to the effect, "do not expire/flush this until done manually or till this times out"; would that cover this use case? BTW I am grateful for the code you wrote, I will be incorporating it, since as you stated, covering all use cases is nearly impossible. Cache expiration is just so complicated... I hope I don't over load the admin's with way too many settings (I know I'm close to that threshold...).

#5

dbeall - October 16, 2009 - 13:37

I was watching this in case it needs to go in Boost tips & tricks.

@mickytown2, that's a good point. You are such a wizard and try to fill every need, we could see a huge configure page. ..It may be a good thing that I don't know php at this point....
- This may be bordering on project/Boost and project/advanced_Boost -

#6

mikeytown2 - October 21, 2009 - 01:20
Status:needs review» fixed

Committed the file with some minor changes.

@arcaic
How do you configure this rule?

#7

dbeall - October 21, 2009 - 02:21

yes please, documentation needs to include this..
will play with it soon, when time permits.

#8

dbeall - October 22, 2009 - 05:46

I have this working..
Will document soon and add to advanced help files.

#9

System Message - November 5, 2009 - 05:50
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.