ChainedStorage.php in Supercache 2.0.x
File
src/KeyValueStore/ChainedStorage.php
View source
<?php
namespace Drupal\supercache\KeyValueStore;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Query\Merge;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\KeyValueStore\DatabaseStorage as KeyValueDatabaseStorage;
use Drupal\Core\KeyValueStore\StorageBase;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\supercache\Cache\DummyTagChecksum;
class ChainedStorage extends KeyValueDatabaseStorage implements KeyValueStoreInterface {
use ChainedStorageTrait;
public function __construct(CacheFactoryInterface $factory, $collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value') {
parent::__construct($collection, $serializer, $connection, $table);
$sanitized_collection = preg_replace('/[^A-Za-z0-9_]+/', '_', $collection);
$this->cache = $factory
->get($table . '_' . $sanitized_collection);
}
public function has($key) {
if ($cache = $this->cache
->get($key)) {
if (!empty($this
->CacheToKeyValue([
$cache,
]))) {
return TRUE;
}
}
return parent::has($key);
}
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;
}
public function getAll() {
$result = parent::getAll();
return $result;
}
public function set($key, $value) {
$this->cache
->set($key, $value);
parent::set($key, $value);
}
public function setIfNotExists($key, $value) {
$result = parent::setIfNotExists($key, $value);
if ($result == Merge::STATUS_INSERT) {
$this->cache
->set($key, $value);
}
return $result;
}
public function rename($key, $new_key) {
parent::rename($key, $new_key);
$this->cache
->delete($key);
}
public function deleteMultiple(array $keys) {
parent::deleteMultiple($keys);
$this->cache
->deleteMultiple($keys);
}
public function deleteAll() {
parent::deleteAll();
$this->cache
->deleteAll();
}
}
Classes
Name |
Description |
ChainedStorage |
Defines a chained key value storage that uses
any cache backend on top of the database default
key/value storage. |