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\DatabaseStorageExpirable as KeyValueDatabaseStorageExpirable;
use Drupal\Core\KeyValueStore\StorageBase;
use Drupal\Core\KeyValueStore\DatabaseStorage;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\supercache\Cache\DummyTagChecksum;
class ChainedStorageExpirable extends KeyValueDatabaseStorageExpirable implements KeyValueStoreExpirableInterface {
use ChainedStorageTrait;
public function __construct(CacheFactoryInterface $factory, $collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
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 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;
}
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
->getExpirations(array_keys($persisted))));
}
$this
->populateMissingValuesLocal($keys, $persisted);
}
$result = $cached + $persisted;
return $result;
}
public function getAll() {
return parent::getAll();
}
function setWithExpire($key, $value, $expire) {
$this->cache
->set($key, $value, REQUEST_TIME + $expire);
parent::setWithExpire($key, $value, $expire);
}
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;
}
function setMultipleWithExpire(array $data, $expire) {
foreach ($data as $key => $value) {
$this
->setWithExpire($key, $value, $expire);
}
}
public function deleteMultiple(array $keys) {
parent::deleteMultiple($keys);
$this->cache
->deleteMultiple($keys);
}
public function deleteAll() {
parent::deleteAll();
$this->cache
->deleteAll();
}
}