Periodically I need to clear out my sites cache. I have had people report weird page content displayed on thier browsers when they access the site when the cache is being cleared.
What is the best way to deal with this?

Comments

afreen.k’s picture

subscribing......

bboldi’s picture

Just create a simple module, implement the cron hook ( http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... ), and use cache_clear_all ( http://api.drupal.org/api/drupal/includes--cache.inc/function/cache_clea... ) to clear cache on every cron run. Like this:


function your_module_cron()
{
   clear_cache_all();
}

gony’s picture

I actually call drupal_flush_all_caches (should I be using cache_clear_all?).

But my question was about what can I don to prevent users who goto my siste when the cache is being cleared being served incomplete ("weird looking") pages?

bboldi’s picture

Oh, sorry. My bad ... I should've read your messages more carefully :) Yes drupal_flush_all_caches is a good choice, it will not only clear the cache but rebuild theme registry and menu structure - but you probably knew that already .

Users getting an incomplete page during the cache clearing procedure is an interesting phenomenon. The strange thing is, that it should not take too long, so the odds of a user visiting the page right when the cache is being cleared are very small. I have never faced something like that... I assume that either:

a) you have a very large site, with many nodes, so it needs more time to empty all the caches and rebuild everything
b) you have many visitors, so the odds of visits during cache clearing procedure are larger
c) you empty the cache too frequently or
d) you are using poorman's cron, and the user gets an incomplete page when poorman's cron runs cache clear procedure

OR non of the above is true, but the server load is higher and it causes cache flush to run slowly ...

This is just an idea, not sure if it helps:

Try to create a separate PHP file on the root and give it a hard to guess name for example: hard-to-guess-cache-clear.php, add the following content:


// include bootstrap
include_once('./includes/bootstrap.inc');

// initialize stuff
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// clear cache
drupal_flush_all_caches();

create another cron job, and run the hard-to-guess-cache-clear.php independently from your drupal page (and of course disable your in-module cache clear solution) ... maybe this way you can avoid weird looking pages ...

gony’s picture

Thanks for this - why would doing this prevent weird looking pages?

bboldi’s picture

Well - because I don't know every aspect of your problem I'm just guessing, you know trying to give you ideas, so separating a maintenance script from the actual script which renders the content can be good idea - and can solve problems like your's - in few cases, for example:

1) if the cron is beeing executed too often, then you can set a separate cron job with longer interval for the script
2) if cron is started from within drupal page render script the separation approach can help with weird looking pages

I hope I helped.

gony’s picture

Many thanks!

lmeurs’s picture

Very handy snippet!

In Drupal 7 I needed to define DRUPAL_ROOT first, so ended up with:

<?php
// define static var
define('DRUPAL_ROOT', getcwd());

// include bootstrap
include_once('./includes/bootstrap.inc');

// initialize stuff
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// clear cache
drupal_flush_all_caches();
?>

Laurens Meurs
wiedes.nl

bboldi’s picture

Oh, I missed that line :) Thanks for the correction!