You are here

class ChainedStorageExpirable in Supercache 8

Same name and namespace in other branches
  1. 2.0.x src/KeyValueStore/ChainedStorageExpirable.php \Drupal\supercache\KeyValueStore\ChainedStorageExpirable

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 ChainedStorageExpirable

File

src/KeyValueStore/ChainedStorageExpirable.php, line 30
Contains \Drupal\supercache\KeyValueStore\ChainedStorageExpirable.

Namespace

Drupal\supercache\KeyValueStore
View source
class ChainedStorageExpirable extends KeyValueDatabaseStorageExpirable implements KeyValueStoreExpirableInterface {
  use ChainedStorageTrait;

  /**
   * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
   *
   * @param CacheFactoryInterface $factory
   *   The cache backend factory.
   * @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.
   */
  public function __construct(CacheFactoryInterface $factory, $collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
    parent::__construct($collection, $serializer, $connection, $table);

    // Make sure the collection name passed to the cache factory
    // does not have any dots, or else if using database storage it will
    // crash.
    $sanitized_collection = preg_replace('/[^A-Za-z0-9_]+/', '_', $collection);
    $this->cache = $factory
      ->get($table . '_' . $sanitized_collection);
  }

  /**
   * {@inheritdoc}
   */
  public function has($key) {
    if ($cache = $this->cache
      ->get($key)) {
      if (!empty($this
        ->CacheToKeyValue([
        $cache,
      ]))) {
        return TRUE;
      }
    }

    // The fact that it does not exist in the cache
    // does not mean it does not exist in the database.
    return parent::has($key);
  }

  /**
   * Retrieve the expiration times for the
   * keys defined in $keys.
   *
   * @param string[] $keys
   * @return int[]
   */
  public function getExpirations(array $keys) {
    $values = $this->connection
      ->query('SELECT name, expire FROM {' . $this->connection
      ->escapeTable($this->table) . '} WHERE name IN ( :keys[] ) AND collection = :collection', array(
      ':keys[]' => $keys,
      ':collection' => $this->collection,
    ))
      ->fetchAllKeyed();
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(array $keys) {
    $cached = [];
    if ($cache = $this->cache
      ->getMultiple($keys)) {
      $cached = $this
        ->CacheToKeyValue($cache);
    }
    $persisted = [];
    if (!empty($keys)) {
      $persisted = parent::getMultiple($keys);
      if (!empty($persisted)) {

        // In order to populate the cache we need to know what the
        // expiration times for each of these key value pairs are,
        // and the DatabaseStorageExpirable implementation does
        // not provide this.
        $this->cache
          ->setMultiple($this
          ->KeyValueToCache($persisted, $this
          ->getExpirations(array_keys($persisted))));
      }
      $this
        ->populateMissingValuesLocal($keys, $persisted);
    }
    $result = $cached + $persisted;
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getAll() {

    // We cannot trust the cache
    // to have everything in it.
    return parent::getAll();
  }

  /**
   * {@inheritdoc}
   */
  function setWithExpire($key, $value, $expire) {
    $this->cache
      ->set($key, $value, REQUEST_TIME + $expire);
    parent::setWithExpire($key, $value, $expire);
  }

  /**
   * {@inheritdoc}
   */
  function setWithExpireIfNotExists($key, $value, $expire) {
    $result = parent::setWithExpireIfNotExists($key, $value, $expire);
    if ($result == Merge::STATUS_INSERT) {
      $this->cache
        ->set($key, $value, REQUEST_TIME + $expire);
    }
    return $result;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {
    parent::deleteAll();
    $this->cache
      ->deleteAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChainedStorageExpirable::deleteAll public function Deletes all items from the key/value store. Overrides DatabaseStorage::deleteAll
ChainedStorageExpirable::deleteMultiple public function Deletes multiple items from the key/value store. Overrides DatabaseStorageExpirable::deleteMultiple
ChainedStorageExpirable::getAll public function Returns all stored key/value pairs in the collection. Overrides DatabaseStorageExpirable::getAll
ChainedStorageExpirable::getExpirations public function Retrieve the expiration times for the keys defined in $keys.
ChainedStorageExpirable::getMultiple public function Returns the stored key/value pairs for a given set of keys. Overrides DatabaseStorageExpirable::getMultiple
ChainedStorageExpirable::has public function Returns whether a given key exists in the store. Overrides DatabaseStorageExpirable::has
ChainedStorageExpirable::setMultipleWithExpire function Saves an array of values with a time to live. Overrides DatabaseStorageExpirable::setMultipleWithExpire
ChainedStorageExpirable::setWithExpire function Saves a value for a given key with a time to live. Overrides DatabaseStorageExpirable::setWithExpire
ChainedStorageExpirable::setWithExpireIfNotExists function Sets a value for a given key with a time to live if it does not yet exist. Overrides DatabaseStorageExpirable::setWithExpireIfNotExists
ChainedStorageExpirable::__construct public function Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides DatabaseStorageExpirable::__construct
ChainedStorageTrait::$cache protected property The cache backend.
ChainedStorageTrait::$EMPTY_VALUE protected property Special value stored in the cache layer to identify missing items in the persistent cache.
ChainedStorageTrait::CacheToKeyValue protected function Converts a cache array to a KeyValue array.
ChainedStorageTrait::KeyValueToCache protected function Converts an array of KeyValues to a cache compatible array.
ChainedStorageTrait::populateMissingValuesLocal protected function To prevent database lookups we store a special 'empty' object.
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::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
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