You are here

class ChainedStorage in Supercache 2.0.x

Same name and namespace in other branches
  1. 8 src/KeyValueStore/ChainedStorage.php \Drupal\supercache\KeyValueStore\ChainedStorage

Defines a chained key value storage that uses any cache backend on top of the database default key/value storage.

This cache backend MUST be a centralized one such as Couchbase, or something that coordinates invalidations such as ChainedFast.

Hierarchy

Expanded class hierarchy of ChainedStorage

File

src/KeyValueStore/ChainedStorage.php, line 33
Contains \Drupal\supercache\KeyValueStore\ChainedStorage;

Namespace

Drupal\supercache\KeyValueStore
View source
class ChainedStorage extends KeyValueDatabaseStorage implements KeyValueStoreInterface {
  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') {
    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);
  }

  /**
   * {@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)) {
        $this->cache
          ->setMultiple($this
          ->KeyValueToCache($persisted));
      }
      $this
        ->populateMissingValuesLocal($keys, $persisted);
    }
    $result = array_merge($cached, $persisted);
    return $result;
  }

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

    // We have to rely on the persistent
    // storage because we cannot rely
    // on the cache layer to have all
    // the key/value pairs.
    $result = parent::getAll();

    // Do not call set multiple here to prepopulate the cache
    // because it will INVALIDATE the binary on ChainedFast
    // $this->cache->setMultiple($this->KeyValueToCache($result));
    return $result;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function rename($key, $new_key) {
    parent::rename($key, $new_key);
    $this->cache
      ->delete($key);
  }

  /**
   * {@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
ChainedStorage::deleteAll public function Deletes all items from the key/value store. Overrides DatabaseStorage::deleteAll
ChainedStorage::deleteMultiple public function Deletes multiple items from the key/value store. Overrides DatabaseStorage::deleteMultiple
ChainedStorage::getAll public function Returns all stored key/value pairs in the collection. Overrides DatabaseStorage::getAll
ChainedStorage::getMultiple public function Returns the stored key/value pairs for a given set of keys. Overrides DatabaseStorage::getMultiple
ChainedStorage::has public function Returns whether a given key exists in the store. Overrides DatabaseStorage::has
ChainedStorage::rename public function Renames a key. Overrides DatabaseStorage::rename
ChainedStorage::set public function Saves a value for a given key. Overrides DatabaseStorage::set
ChainedStorage::setIfNotExists public function Saves a value for a given key if it does not exist yet. Overrides DatabaseStorage::setIfNotExists
ChainedStorage::__construct public function Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides DatabaseStorage::__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::catchException protected function Act on an exception when the table might not have been created.
DatabaseStorage::doSet protected function Saves a value for a given key.
DatabaseStorage::doSetIfNotExists public function Saves a value for a given key if it does not exist yet.
DatabaseStorage::ensureTableExists protected function Check if the table exists and create it if not.
DatabaseStorage::schemaDefinition public static function Defines the schema for the key_value table. 1
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
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