You are here

function cache_clear_all in Memcache API and Integration 6

Same name in this branch
  1. 6 memcache.db.inc \cache_clear_all()
  2. 6 memcache.inc \cache_clear_all()
Same name and namespace in other branches
  1. 5.2 memcache.db.inc \cache_clear_all()
  2. 5.2 memcache.inc \cache_clear_all()
  3. 5 memcache.db.inc \cache_clear_all()
  4. 5 memcache.inc \cache_clear_all()

Expire data from the cache. If called without arguments, expirable entries will be cleared from the cache_page table.

Parameters

$cid: If set, the cache ID to delete. Otherwise, all cache entries that can expire are deleted.

$table: If set, the table $table to delete from. Mandatory argument if $cid is set.

$wildcard: If $wildcard is TRUE, cache IDs starting with $cid are deleted in addition to the exact cache ID specified by $cid. If $wildcard is TRUE and $cid is '*' then the entire table $table is emptied.

7 calls to cache_clear_all()
filter_user in ./memcache-session.inc
Implement hook_user() using a required module's namespace since memcache is not a module and thus can't implement hooks directly.
MemCacheClearCase::clearCidTest in tests/memcache.test
Test clearing using a cid.
MemCacheClearCase::clearWildcardPrefixTest in tests/memcache.test
Test cache clears using wildcard prefixes.
MemCacheClearCase::testClearCacheLifetime in tests/memcache.test
Test full bin flushes with cache lifetime.
MemCacheClearCase::testClearWildcardFull in tests/memcache.test
Test full bin flushes with no cache lifetime.

... See full list

File

./memcache.db.inc, line 185
Implementation of cache.inc with memcache logic included.

Code

function cache_clear_all($cid = NULL, $table = NULL, $wildcard = FALSE) {
  global $user;

  // Default behavior for when cache_clear_all() is called without parameters
  // is to clear all of the expirable entries in the block and page caches.
  if (!isset($cid) && !isset($table)) {

    // Clear the block cache first, so stale data will
    // not end up in the page cache.
    cache_clear_all(NULL, 'cache_block');
    cache_clear_all(NULL, 'cache_page');
    return;
  }
  if (empty($cid)) {
    if (variable_get('cache_lifetime', 0)) {

      // We store the time in the current user's $user->cache variable which
      // will be saved into the sessions table by sess_write(). We then
      // simulate that the cache was flushed for this user by not returning
      // cached data that was cached before the timestamp.
      $user->cache = $_SERVER['REQUEST_TIME'];
      $cache_flush = variable_get('cache_flush_' . $table, 0);
      if ($cache_flush == 0) {

        // This is the first request to clear the cache, start a timer.
        variable_set('cache_flush_' . $table, $_SERVER['REQUEST_TIME']);
      }
      else {
        if ($_SERVER['REQUEST_TIME'] > $cache_flush + variable_get('cache_lifetime', 0)) {

          // Clear the cache for everyone, cache_lifetime seconds have
          // passed since the first request to clear the cache.
          db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, $_SERVER['REQUEST_TIME']);
          variable_set('cache_flush_' . $table, 0);

          // Note there is no memcache flush here.
          // Items are cached in memcache with an expiry equal to cache_lifetime
          // and memcache will expire these automatically.
        }
      }
    }
    else {

      // No minimum cache lifetime, flush all temporary cache entries now.
      // Note, however, that memcache does not have the ability to discern
      // between CACHE_PERMANENT and CACHE_TEMPORARY items. So having no
      // minimum cache lifetime means that all CACHE_PERMANENT items are
      // lost here, too. To avoid that, set a minimum cache lifetime.
      dmemcache_flush($table);
      db_query("DELETE FROM {" . $table . "} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, $_SERVER['REQUEST_TIME']);
    }
  }
  else {
    if ($wildcard) {
      if ($cid == '*') {
        dmemcache_flush($table);
        db_query("TRUNCATE TABLE {" . $table . "}");
      }
      else {
        db_query("DELETE FROM {" . $table . "} WHERE cid LIKE '%s%%'", $cid);
      }
    }
    else {
      dmemcache_delete($cid, $table);
      db_query("DELETE FROM {" . $table . "} WHERE cid = '%s'", $cid);
    }
  }
}