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)
/**
* 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
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | boost-605568.patch | 798 bytes | mikeytown2 |
Comments
Comment #1
dbeall commentedwatching this thread.. (have not tried it yet), sounds neat!
Comment #2
mikeytown2 commentedUsage 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...
Comment #3
arcaicHi, 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
Comment #4
mikeytown2 commentedInteresting 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...).
Comment #5
dbeall commentedI 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 -
Comment #6
mikeytown2 commentedCommitted the file with some minor changes.
@arcaic
How do you configure this rule?
Comment #7
dbeall commentedyes please, documentation needs to include this..
will play with it soon, when time permits.
Comment #8
dbeall commentedI have this working..
Will document soon and add to advanced help files.
Comment #10
Tarsjusz commentedI had two problems while trying to make it work, both with line
if (boost_is_cached($path)) {
1. Shouldn't 'path' be 'page'?
2. My rule was executed on page, which had a query on url address (example.com/blabla?sth=12)
boost_is_cached uses method to check if page is cached, that adds this query no matter if this makes any sense or not. In my case: it doesn't. So I had to change this line to:
if(file_exists(boost_file_path($page, false))) {
Comment #11
digi24 commentedAs far as I see, the problem addressed by Tarjusz is still present in the code, the rule cannot work.The simple solution suggested in #10 however will not work with URL-aliases.
Comment #12
mikeytown2 commentedproblem with features you never use...
Comment #13
mikeytown2 commentedComment #14
codemannAbove patch works perfectly, the insert works now, and the update also works perfectly.
But when I delete a node, the cached view pages associated with it don't get cleared.
Comment #15
mikeytown2 commented#896188: Add option in Rules action to clear cache file immediately
Comment #16
diego.pasc commentedFirst of all....Boost rules really rock! Thanks.
It would be nice to have the possibility to clear more URLs from the same textarea (one URL per line for example) in the rule settings.