class DatabaseBackend

Same name in this branch

Defines a default cache implementation.

This is Drupal's default cache implementation. It uses the database to store cached data. Each cache bin corresponds to a database table by the same name.

Hierarchy

Expanded class hierarchy of DatabaseBackend

2 files declare their use of DatabaseBackend
DatabaseBackendUnitTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Cache/DatabaseBackendUnitTest.php
Definition of Drupal\system\Tests\Cache\DatabaseBackendUnitTest.
InstallTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Cache/InstallTest.php
Definition of Drupal\system\Tests\Cache\InstallTest.

File

drupal/core/lib/Drupal/Core/Cache/DatabaseBackend.php, line 20
Definition of Drupal\Core\Cache\DatabaseBackend.

Namespace

Drupal\Core\Cache
View source
class DatabaseBackend implements CacheBackendInterface {

  /**
   * @var string
   */
  protected $bin;

  /**
   * Constructs a DatabaseBackend object.
   *
   * @param string $bin
   *   The cache bin for which the object is created.
   */
  public function __construct($bin) {

    // All cache tables should be prefixed with 'cache_', except for the
    // default 'cache' bin.
    if ($bin != 'cache') {
      $bin = 'cache_' . $bin;
    }
    $this->bin = $bin;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::get().
   */
  public function get($cid, $allow_invalid = FALSE) {
    $cids = array(
      $cid,
    );
    $cache = $this
      ->getMultiple($cids, $allow_invalid);
    return reset($cache);
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple().
   */
  public function getMultiple(&$cids, $allow_invalid = FALSE) {
    try {

      // When serving cached pages, the overhead of using ::select() was found
      // to add around 30% overhead to the request. Since $this->bin is a
      // variable, this means the call to ::query() here uses a concatenated
      // string. This is highly discouraged under any other circumstances, and
      // is used here only due to the performance overhead we would incur
      // otherwise. When serving an uncached page, the overhead of using
      // ::select() is a much smaller proportion of the request.
      $result = Database::getConnection()
        ->query('SELECT cid, data, created, expire, serialized, tags, checksum_invalidations, checksum_deletions FROM {' . Database::getConnection()
        ->escapeTable($this->bin) . '} WHERE cid IN (:cids)', array(
        ':cids' => $cids,
      ));
      $cache = array();
      foreach ($result as $item) {
        $item = $this
          ->prepareItem($item, $allow_invalid);
        if ($item) {
          $cache[$item->cid] = $item;
        }
      }
      $cids = array_diff($cids, array_keys($cache));
      return $cache;
    } catch (Exception $e) {

      // If the database is never going to be available, cache requests should
      // return FALSE in order to allow exception handling to occur.
      return array();
    }
  }

  /**
   * Prepares a cached item.
   *
   * Checks that items are either permanent or did not expire, and unserializes
   * data as appropriate.
   *
   * @param stdClass $cache
   *   An item loaded from cache_get() or cache_get_multiple().
   * @param bool $allow_invalid
   *   If FALSE, the method returns FALSE if the cache item is not valid.
   *
   * @return mixed|false
   *   The item with data unserialized as appropriate and a property indicating
   *   whether the item is valid, or FALSE if there is no valid item to load.
   */
  protected function prepareItem($cache, $allow_invalid) {
    if (!isset($cache->data)) {
      return FALSE;
    }
    $cache->tags = $cache->tags ? explode(' ', $cache->tags) : array();
    $checksum = $this
      ->checksumTags($cache->tags);

    // Check if deleteTags() has been called with any of the entry's tags.
    if ($cache->checksum_deletions != $checksum['deletions']) {
      return FALSE;
    }

    // Check expire time.
    $cache->valid = $cache->expire == CacheBackendInterface::CACHE_PERMANENT || $cache->expire >= REQUEST_TIME;

    // Check if invalidateTags() has been called with any of the entry's tags.
    if ($cache->checksum_invalidations != $checksum['invalidations']) {
      $cache->valid = FALSE;
    }
    if (!$allow_invalid && !$cache->valid) {
      return FALSE;
    }

    // Unserialize and return the cached data.
    if ($cache->serialized) {
      $cache->data = unserialize($cache->data);
    }
    return $cache;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::set().
   */
  public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array()) {
    try {
      $flat_tags = $this
        ->flattenTags($tags);
      $checksum = $this
        ->checksumTags($flat_tags);
      $fields = array(
        'serialized' => 0,
        'created' => REQUEST_TIME,
        'expire' => $expire,
        'tags' => implode(' ', $flat_tags),
        'checksum_invalidations' => $checksum['invalidations'],
        'checksum_deletions' => $checksum['deletions'],
      );
      if (!is_string($data)) {
        $fields['data'] = serialize($data);
        $fields['serialized'] = 1;
      }
      else {
        $fields['data'] = $data;
        $fields['serialized'] = 0;
      }
      Database::getConnection()
        ->merge($this->bin)
        ->key(array(
        'cid' => $cid,
      ))
        ->fields($fields)
        ->execute();
    } catch (Exception $e) {

      // The database may not be available, so we'll ignore cache_set requests.
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::delete().
   */
  public function delete($cid) {
    Database::getConnection()
      ->delete($this->bin)
      ->condition('cid', $cid)
      ->execute();
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
   */
  public function deleteMultiple(array $cids) {

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

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags().
   */
  public function deleteTags(array $tags) {
    $tag_cache =& drupal_static('Drupal\\Core\\Cache\\CacheBackendInterface::tagCache');
    foreach ($this
      ->flattenTags($tags) as $tag) {
      unset($tag_cache[$tag]);
      Database::getConnection()
        ->merge('cache_tags')
        ->insertFields(array(
        'deletions' => 1,
      ))
        ->expression('deletions', 'deletions + 1')
        ->key(array(
        'tag' => $tag,
      ))
        ->execute();
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll().
   */
  public function deleteAll() {
    Database::getConnection()
      ->truncate($this->bin)
      ->execute();
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteExpired().
   */
  public function deleteExpired() {
    Database::getConnection()
      ->delete($this->bin)
      ->condition('expire', CacheBackendInterface::CACHE_PERMANENT, '<>')
      ->condition('expire', REQUEST_TIME, '<')
      ->execute();
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidate().
   */
  public function invalidate($cid) {
    $this
      ->invalidateMultiple(array(
      $cid,
    ));
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalideMultiple().
   */
  public function invalidateMultiple(array $cids) {

    // Update in chunks when a large array is passed.
    do {
      Database::getConnection()
        ->update($this->bin)
        ->fields(array(
        'expire' => REQUEST_TIME - 1,
      ))
        ->condition('cid', array_splice($cids, 0, 1000), 'IN')
        ->execute();
    } while (count($cids));
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
   */
  public function invalidateTags(array $tags) {
    $tag_cache =& drupal_static('Drupal\\Core\\Cache\\CacheBackendInterface::tagCache');
    foreach ($this
      ->flattenTags($tags) as $tag) {
      unset($tag_cache[$tag]);
      Database::getConnection()
        ->merge('cache_tags')
        ->insertFields(array(
        'invalidations' => 1,
      ))
        ->expression('invalidations', 'invalidations + 1')
        ->key(array(
        'tag' => $tag,
      ))
        ->execute();
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
   */
  public function invalidateAll() {
    Database::getConnection()
      ->update($this->bin)
      ->fields(array(
      'expire' => REQUEST_TIME - 1,
    ))
      ->execute();
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection().
   */
  public function garbageCollection() {
    $this
      ->deleteExpired();
  }

  /**
   * 'Flattens' a tags array into an array of strings.
   *
   * @param array $tags
   *   Associative array of tags to flatten.
   *
   * @return array
   *   An indexed array of flattened tag identifiers.
   */
  protected function flattenTags(array $tags) {
    if (isset($tags[0])) {
      return $tags;
    }
    $flat_tags = array();
    foreach ($tags as $namespace => $values) {
      if (is_array($values)) {
        foreach ($values as $value) {
          $flat_tags[] = "{$namespace}:{$value}";
        }
      }
      else {
        $flat_tags[] = "{$namespace}:{$values}";
      }
    }
    return $flat_tags;
  }

  /**
   * Returns the sum total of validations for a given set of tags.
   *
   * @param array $tags
   *   Array of flat tags.
   *
   * @return int
   *   Sum of all invalidations.
   *
   * @see Drupal\Core\Cache\DatabaseBackend::flattenTags()
   */
  protected function checksumTags($flat_tags) {
    $tag_cache =& drupal_static('Drupal\\Core\\Cache\\CacheBackendInterface::tagCache', array());
    $checksum = array(
      'invalidations' => 0,
      'deletions' => 0,
    );
    $query_tags = array_diff($flat_tags, array_keys($tag_cache));
    if ($query_tags) {
      $db_tags = Database::getConnection()
        ->query('SELECT tag, invalidations, deletions FROM {cache_tags} WHERE tag IN (:tags)', array(
        ':tags' => $query_tags,
      ))
        ->fetchAllAssoc('tag', PDO::FETCH_ASSOC);
      $tag_cache += $db_tags;

      // Fill static cache with empty objects for tags not found in the database.
      $tag_cache += array_fill_keys(array_diff($query_tags, array_keys($db_tags)), $checksum);
    }
    foreach ($flat_tags as $tag) {
      $checksum['invalidations'] += $tag_cache[$tag]['invalidations'];
      $checksum['deletions'] += $tag_cache[$tag]['deletions'];
    }
    return $checksum;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty().
   */
  public function isEmpty() {
    $this
      ->garbageCollection();
    $query = Database::getConnection()
      ->select($this->bin);
    $query
      ->addExpression('1');
    $result = $query
      ->range(0, 1)
      ->execute()
      ->fetchField();
    return empty($result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
DatabaseBackend::$bin protected property
DatabaseBackend::checksumTags protected function Returns the sum total of validations for a given set of tags.
DatabaseBackend::delete public function Implements Drupal\Core\Cache\CacheBackendInterface::delete(). Overrides CacheBackendInterface::delete 1
DatabaseBackend::deleteAll public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll(). Overrides CacheBackendInterface::deleteAll 1
DatabaseBackend::deleteExpired public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteExpired(). Overrides CacheBackendInterface::deleteExpired 1
DatabaseBackend::deleteMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple(). Overrides CacheBackendInterface::deleteMultiple 1
DatabaseBackend::deleteTags public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags(). Overrides CacheBackendInterface::deleteTags 1
DatabaseBackend::flattenTags protected function 'Flattens' a tags array into an array of strings.
DatabaseBackend::garbageCollection public function Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection(). Overrides CacheBackendInterface::garbageCollection 1
DatabaseBackend::get public function Implements Drupal\Core\Cache\CacheBackendInterface::get(). Overrides CacheBackendInterface::get 1
DatabaseBackend::getMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple(). Overrides CacheBackendInterface::getMultiple 1
DatabaseBackend::invalidate public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidate(). Overrides CacheBackendInterface::invalidate 1
DatabaseBackend::invalidateAll public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). Overrides CacheBackendInterface::invalidateAll 1
DatabaseBackend::invalidateMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::invalideMultiple(). Overrides CacheBackendInterface::invalidateMultiple 1
DatabaseBackend::invalidateTags public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). Overrides CacheBackendInterface::invalidateTags 1
DatabaseBackend::isEmpty public function Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty(). Overrides CacheBackendInterface::isEmpty 1
DatabaseBackend::prepareItem protected function Prepares a cached item.
DatabaseBackend::set public function Implements Drupal\Core\Cache\CacheBackendInterface::set(). Overrides CacheBackendInterface::set 1
DatabaseBackend::__construct public function Constructs a DatabaseBackend object.