Drupal coder

An overview of Drupal's cache_clear_all uses

Drupal has a very simple, yet powerful (low level) caching system. It provides 3 functions that should please all you caching needs: cache_get, cache_set and cache_clear_all. The first two are pretty straight forward to use, but the third one might take some time to wrap your head around.

It accepts 3 argument: a cache ID, table cache and a wilcard boolean. Each combination of these 3 has a very different result. Here's an overview of all combinations.

cache_clear_all()

Passing no arguments to cache_clear_all is the same as saying the following

cache_clear_all(NULL, 'cache_block');
cache_clear_all(NULL, 'cache_page');

As you'll learn further on, this will clear all expired entries from the block and page cache. This is for example used when you update a node, update a block etc.

cache_clear_all(NULL, 'cache_TABLE')

Specifying the cache table but passing NULL for the cache ID will remove all expired entries from the specified table.

cache_clear_all('*', 'cache_TABLE', TRUE)

Specifying the cache table but passing '*' for the cache ID will remove all (expired an temporary) entries from the specified table. You will also need to specify that you're using the '*' wildcard by setting the $wildcard parameter to TRUE.

cache_clear_all('KEY_PREFIX', 'cache_TABLE', TRUE)

Specifying the cache table but passing a key prefix for the cache ID will remove all (expired an temporary) entries from the specified table whose cache ID starts with the KEY_PREFIX. You will also need to specify that you're using the prefix by setting the $wildcard parameter to TRUE.

cache_clear_all('KEY', 'cache_TABLE')

By specifying a specific cache ID and cache table (and no $wildcard), you're saying: remove that entry from the table regardless the fact that it's expired or not.

May 19, 2010Drupal, performance

Comments

If nothing works then please use these steps.

$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);
}

Kind regards
Ajay

I`m wondering that this stuff doesn`t work fine always. Sometimes the only solution going to mysql query with deletion of content like this:

db_query('DELETE FROM cache_content WHERE cid = \'%s\'', 'content:' . $node->nid . ':' . $node->vid);

PS: don`t warry for injective variables.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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>
  • Lines and paragraphs break automatically.

More information about formatting options