You are here

class DatabaseStorageExpirable in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php \Drupal\Core\KeyValueStore\DatabaseStorageExpirable

Defines a default key/value store implementation for expiring items.

This key/value store implementation uses the database to store key/value data with an expire date.

Hierarchy

Expanded class hierarchy of DatabaseStorageExpirable

1 file declares its use of DatabaseStorageExpirable
GarbageCollectionTest.php in core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php

File

core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php, line 14

Namespace

Drupal\Core\KeyValueStore
View source
class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreExpirableInterface {

  /**
   * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
   *
   * @param string $collection
   *   The name of the collection holding key and value pairs.
   * @param \Drupal\Component\Serialization\SerializationInterface $serializer
   *   The serialization class to use.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to use.
   * @param string $table
   *   The name of the SQL table to use, defaults to key_value_expire.
   */
  public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
    parent::__construct($collection, $serializer, $connection, $table);
  }

  /**
   * {@inheritdoc}
   */
  public function has($key) {
    return (bool) $this->connection
      ->query('SELECT 1 FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key AND expire > :now', [
      ':collection' => $this->collection,
      ':key' => $key,
      ':now' => REQUEST_TIME,
    ])
      ->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(array $keys) {
    $values = $this->connection
      ->query('SELECT name, value FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE expire > :now AND name IN ( :keys[] ) AND collection = :collection', [
      ':now' => REQUEST_TIME,
      ':keys[]' => $keys,
      ':collection' => $this->collection,
    ])
      ->fetchAllKeyed();
    return array_map([
      $this->serializer,
      'decode',
    ], $values);
  }

  /**
   * {@inheritdoc}
   */
  public function getAll() {
    $values = $this->connection
      ->query('SELECT name, value FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now', [
      ':collection' => $this->collection,
      ':now' => REQUEST_TIME,
    ])
      ->fetchAllKeyed();
    return array_map([
      $this->serializer,
      'decode',
    ], $values);
  }

  /**
   * {@inheritdoc}
   */
  public function setWithExpire($key, $value, $expire) {
    $this->connection
      ->merge($this->table)
      ->keys([
      'name' => $key,
      'collection' => $this->collection,
    ])
      ->fields([
      'value' => $this->serializer
        ->encode($value),
      'expire' => REQUEST_TIME + $expire,
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function setWithExpireIfNotExists($key, $value, $expire) {
    if (!$this
      ->has($key)) {
      $this
        ->setWithExpire($key, $value, $expire);
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setMultipleWithExpire(array $data, $expire) {
    foreach ($data as $key => $value) {
      $this
        ->setWithExpire($key, $value, $expire);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMultiple(array $keys) {
    parent::deleteMultiple($keys);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStorage::$connection protected property The database connection.
DatabaseStorage::$serializer protected property The serialization class to use.
DatabaseStorage::$table protected property The name of the SQL table to use.
DatabaseStorage::deleteAll public function Deletes all items from the key/value store. Overrides KeyValueStoreInterface::deleteAll
DatabaseStorage::rename public function Renames a key. Overrides KeyValueStoreInterface::rename
DatabaseStorage::set public function Saves a value for a given key. Overrides KeyValueStoreInterface::set
DatabaseStorage::setIfNotExists public function Saves a value for a given key if it does not exist yet. Overrides KeyValueStoreInterface::setIfNotExists
DatabaseStorageExpirable::deleteMultiple public function Deletes multiple items from the key/value store. Overrides DatabaseStorage::deleteMultiple
DatabaseStorageExpirable::getAll public function Returns all stored key/value pairs in the collection. Overrides DatabaseStorage::getAll
DatabaseStorageExpirable::getMultiple public function Returns the stored key/value pairs for a given set of keys. Overrides DatabaseStorage::getMultiple
DatabaseStorageExpirable::has public function Returns whether a given key exists in the store. Overrides DatabaseStorage::has
DatabaseStorageExpirable::setMultipleWithExpire public function Saves an array of values with a time to live. Overrides KeyValueStoreExpirableInterface::setMultipleWithExpire
DatabaseStorageExpirable::setWithExpire public function Saves a value for a given key with a time to live. Overrides KeyValueStoreExpirableInterface::setWithExpire
DatabaseStorageExpirable::setWithExpireIfNotExists public function Sets a value for a given key with a time to live if it does not yet exist. Overrides KeyValueStoreExpirableInterface::setWithExpireIfNotExists
DatabaseStorageExpirable::__construct public function Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides DatabaseStorage::__construct
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
StorageBase::$collection protected property The name of the collection holding key and value pairs.
StorageBase::delete public function Deletes an item from the key/value store. Overrides KeyValueStoreInterface::delete 1
StorageBase::get public function Returns the stored value for a given key. Overrides KeyValueStoreInterface::get 1
StorageBase::getCollectionName public function Returns the name of this collection. Overrides KeyValueStoreInterface::getCollectionName
StorageBase::setMultiple public function Saves key/value pairs. Overrides KeyValueStoreInterface::setMultiple 1