class ChainedStorageExpirable in Supercache 8
Same name and namespace in other branches
- 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
- class \Drupal\Core\KeyValueStore\StorageBase implements KeyValueStoreInterface
- class \Drupal\Core\KeyValueStore\DatabaseStorage uses DependencySerializationTrait
- class \Drupal\Core\KeyValueStore\DatabaseStorageExpirable implements KeyValueStoreExpirableInterface
- class \Drupal\supercache\KeyValueStore\ChainedStorageExpirable implements KeyValueStoreExpirableInterface uses ChainedStorageTrait
- class \Drupal\Core\KeyValueStore\DatabaseStorageExpirable implements KeyValueStoreExpirableInterface
- class \Drupal\Core\KeyValueStore\DatabaseStorage uses DependencySerializationTrait
Expanded class hierarchy of ChainedStorageExpirable
File
- src/
KeyValueStore/ ChainedStorageExpirable.php, line 30 - Contains \Drupal\supercache\KeyValueStore\ChainedStorageExpirable.
Namespace
Drupal\supercache\KeyValueStoreView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChainedStorageExpirable:: |
public | function |
Deletes all items from the key/value store. Overrides DatabaseStorage:: |
|
ChainedStorageExpirable:: |
public | function |
Deletes multiple items from the key/value store. Overrides DatabaseStorageExpirable:: |
|
ChainedStorageExpirable:: |
public | function |
Returns all stored key/value pairs in the collection. Overrides DatabaseStorageExpirable:: |
|
ChainedStorageExpirable:: |
public | function | Retrieve the expiration times for the keys defined in $keys. | |
ChainedStorageExpirable:: |
public | function |
Returns the stored key/value pairs for a given set of keys. Overrides DatabaseStorageExpirable:: |
|
ChainedStorageExpirable:: |
public | function |
Returns whether a given key exists in the store. Overrides DatabaseStorageExpirable:: |
|
ChainedStorageExpirable:: |
function |
Saves an array of values with a time to live. Overrides DatabaseStorageExpirable:: |
||
ChainedStorageExpirable:: |
function |
Saves a value for a given key with a time to live. Overrides DatabaseStorageExpirable:: |
||
ChainedStorageExpirable:: |
function |
Sets a value for a given key with a time to live if it does not yet exist. Overrides DatabaseStorageExpirable:: |
||
ChainedStorageExpirable:: |
public | function |
Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides DatabaseStorageExpirable:: |
|
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. | |
DatabaseStorage:: |
public | function |
Renames a key. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Saves a value for a given key. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Saves a value for a given key if it does not exist yet. Overrides KeyValueStoreInterface:: |
|
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 |