You are here

function DrupalDatabaseCache::clear in Drupal 7

Implements DrupalCacheInterface::clear().

Overrides DrupalCacheInterface::clear

1 call to DrupalDatabaseCache::clear()
DrupalFakeCache::clear in includes/cache-install.inc
Overrides DrupalDatabaseCache::clear().
1 method overrides DrupalDatabaseCache::clear()
DrupalFakeCache::clear in includes/cache-install.inc
Overrides DrupalDatabaseCache::clear().

File

includes/cache.inc, line 487
Functions and interfaces for cache handling.

Class

DrupalDatabaseCache
Defines a default cache implementation.

Code

function clear($cid = NULL, $wildcard = FALSE) {
  global $user;
  if (empty($cid)) {
    if (variable_get('cache_lifetime', 0)) {

      // We store the time in the current user's session. We then simulate
      // that the cache was flushed for this user by not returning cached
      // data that was cached before the timestamp.
      $_SESSION['cache_expiration'][$this->bin] = REQUEST_TIME;
      $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
      if ($cache_flush == 0) {

        // This is the first request to clear the cache, start a timer.
        variable_set('cache_flush_' . $this->bin, REQUEST_TIME);
      }
      elseif (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_delete($this->bin)
          ->condition('expire', CACHE_PERMANENT, '<>')
          ->condition('expire', REQUEST_TIME, '<')
          ->execute();
        variable_set('cache_flush_' . $this->bin, 0);
      }
    }
    else {

      // No minimum cache lifetime, flush all temporary cache entries now.
      db_delete($this->bin)
        ->condition('expire', CACHE_PERMANENT, '<>')
        ->condition('expire', REQUEST_TIME, '<')
        ->execute();
    }
  }
  else {
    if ($wildcard) {
      if ($cid == '*') {

        // Check if $this->bin is a cache table before truncating. Other
        // cache_clear_all() operations throw a PDO error in this situation,
        // so we don't need to verify them first. This ensures that non-cache
        // tables cannot be truncated accidentally.
        if ($this
          ->isValidBin()) {
          db_truncate($this->bin)
            ->execute();
        }
        else {
          throw new Exception(t('Invalid or missing cache bin specified: %bin', array(
            '%bin' => $this->bin,
          )));
        }
      }
      else {
        db_delete($this->bin)
          ->condition('cid', db_like($cid) . '%', 'LIKE')
          ->execute();
      }
    }
    elseif (is_array($cid)) {

      // Delete in chunks when a large array is passed.
      do {
        db_delete($this->bin)
          ->condition('cid', array_splice($cid, 0, 1000), 'IN')
          ->execute();
      } while (count($cid));
    }
    else {
      db_delete($this->bin)
        ->condition('cid', $cid)
        ->execute();
    }
  }
}