class ChainedStorage in Supercache 8
Same name and namespace in other branches
- 2.0.x 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
- class \Drupal\Core\KeyValueStore\StorageBase implements KeyValueStoreInterface
- class \Drupal\Core\KeyValueStore\DatabaseStorage uses DependencySerializationTrait
- class \Drupal\supercache\KeyValueStore\ChainedStorage implements KeyValueStoreInterface uses ChainedStorageTrait
- class \Drupal\Core\KeyValueStore\DatabaseStorage uses DependencySerializationTrait
Expanded class hierarchy of ChainedStorage
File
- src/
KeyValueStore/ ChainedStorage.php, line 33 - Contains \Drupal\supercache\KeyValueStore\ChainedStorage;
Namespace
Drupal\supercache\KeyValueStoreView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChainedStorage:: |
public | function |
Deletes all items from the key/value store. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Deletes multiple items from the key/value store. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Returns all stored key/value pairs in the collection. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Returns the stored key/value pairs for a given set of keys. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Returns whether a given key exists in the store. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Renames a key. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Saves a value for a given key. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Saves a value for a given key if it does not exist yet. Overrides DatabaseStorage:: |
|
ChainedStorage:: |
public | function |
Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides DatabaseStorage:: |
|
ChainedStorageTrait:: |
protected | property | The cache backend. | |
ChainedStorageTrait:: |
protected | property | Special value stored in the cache layer to identify missing items in the persistent cache. | |
ChainedStorageTrait:: |
protected | function | Converts a cache array to a KeyValue array. | |
ChainedStorageTrait:: |
protected | function | Converts an array of KeyValues to a cache compatible array. | |
ChainedStorageTrait:: |
protected | function | To prevent database lookups we store a special 'empty' object. | |
DatabaseStorage:: |
protected | property | The database connection. | |
DatabaseStorage:: |
protected | property | The serialization class to use. | |
DatabaseStorage:: |
protected | property | The name of the SQL table to use. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
StorageBase:: |
protected | property | The name of the collection holding key and value pairs. | |
StorageBase:: |
public | function |
Deletes an item from the key/value store. Overrides KeyValueStoreInterface:: |
1 |
StorageBase:: |
public | function |
Returns the stored value for a given key. Overrides KeyValueStoreInterface:: |
1 |
StorageBase:: |
public | function |
Returns the name of this collection. Overrides KeyValueStoreInterface:: |
|
StorageBase:: |
public | function |
Saves key/value pairs. Overrides KeyValueStoreInterface:: |
1 |