You are here

protected function DrupalDatabaseCache::garbageCollection in Drupal 7

Garbage collection for get() and getMultiple().

Parameters

$bin: The bin being requested.

2 calls to DrupalDatabaseCache::garbageCollection()
DrupalDatabaseCache::getMultiple in includes/cache.inc
Implements DrupalCacheInterface::getMultiple().
DrupalDatabaseCache::isEmpty in includes/cache.inc
Implements DrupalCacheInterface::isEmpty().

File

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

Class

DrupalDatabaseCache
Defines a default cache implementation.

Code

protected function garbageCollection() {
  $cache_lifetime = variable_get('cache_lifetime', 0);

  // Clean-up the per-user cache expiration session data, so that the session
  // handler can properly clean-up the session data for anonymous users.
  if (isset($_SESSION['cache_expiration'])) {
    $expire = REQUEST_TIME - $cache_lifetime;
    foreach ($_SESSION['cache_expiration'] as $bin => $timestamp) {
      if ($timestamp < $expire) {
        unset($_SESSION['cache_expiration'][$bin]);
      }
    }
    if (!$_SESSION['cache_expiration']) {
      unset($_SESSION['cache_expiration']);
    }
  }

  // Garbage collection of temporary items is only necessary when enforcing
  // a minimum cache lifetime.
  if (!$cache_lifetime) {
    return;
  }

  // When cache lifetime is in force, avoid running garbage collection too
  // often since this will remove temporary cache items indiscriminately.
  $cache_flush = variable_get('cache_flush_' . $this->bin, 0);
  if ($cache_flush && $cache_flush + $cache_lifetime <= REQUEST_TIME) {

    // Reset the variable immediately to prevent a meltdown in heavy load situations.
    variable_set('cache_flush_' . $this->bin, 0);

    // Time to flush old cache data
    db_delete($this->bin)
      ->condition('expire', CACHE_PERMANENT, '<>')
      ->condition('expire', $cache_flush, '<=')
      ->execute();
  }
}