Home

drupalaficionado.com

I love lamp Drupal

Forget about clearing your cache in development

Posted on:
05th Jan '09
Terms:
  • cache
  • drupal 6
  • drupal_flush_all_caches()
  • template.php
  • themeing

Today I downloaded a copy of the Blueprint theme: http://drupal.org/project/blueprint. I had been looking at the blueprint css framework as I have to familiarize myself with it for an upcoming project. As I always do when I find an interesting service or script online, I run to drupal.org and see if it has already been integrated in some way via some contrib module. When I started out with Drupal I got into the nasty habit of building out functionality without checking whether it already existed or not as a contrib module. I've got out of that habit now :).

Anyway - I downloaded and began poking my nose about in the theme files, and in the template.php file I found this beauty:

/**
* Uncomment the following line during development to automatically
* flush the theme cache when you load the page. That way it will
* always look for new tpl files.
*/
// drupal_flush_all_caches();

Fantastic - no more having to click to clear the caches (via the Devel module, or via admin > site configuration > performance). Every time a page is loaded, the cache tables will be cleared.

Here's a full breakdown of drupal_flush_all_caches() from api.drupal.org

Definition

- drupal_flush_all_caches()
- includes/common.inc, line 3627

Description

- Flush all cached data on the site.
- Empties cache tables, rebuilds the menu cache and theme registries, and invokes a hook so that other modules' cache data can be cleared as well.

<?php
function drupal_flush_all_caches() {
 
// Change query-strings on css/js files to enforce reload for all users.
 
_drupal_flush_css_js();

 
drupal_clear_css_cache();
 
drupal_clear_js_cache();

 
// If invoked from update.php, we must not update the theme information in the
  // database, or this will result in all themes being disabled.
 
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
   
_system_theme_data();
  }
  else {
   
system_theme_data();
  }

 
drupal_rebuild_theme_registry();
 
menu_rebuild();
 
node_types_rebuild();
 
// Don't clear cache_form - in-progress form submissions may break.
  // Ordered so clearing the page cache will always be the last action.
 
$core = array('cache', 'cache_block', 'cache_filter', 'cache_page');
 
$cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  foreach (
$cache_tables as $table) {
   
cache_clear_all('*', $table, TRUE);
  }
}
?>

We can see that this clears all cached data on a site, so it's a handy function for module development too.

1 comment

 
#1
Anonymous wrote 1 year 8 weeks ago

Here's another suggestion

Check out this method of skinning the same cat:

http://www.garfieldtech.com/blog/drupal-cache-disable

Add your comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Term Index

  • 'drupal history'
  • cache
  • community
  • dries
  • drupal 5
  • drupal 6
  • drupalcamp
  • drupal_flush_all_caches()
  • gmap
  • location
  • template.php
  • themeing
  • theme_submit()
  • useful modules
  • video
  • views