View source
<?php
namespace Drupal\Core\KeyValueStore;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Connection;
class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreExpirableInterface {
public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
parent::__construct($collection, $serializer, $connection, $table);
}
public function has($key) {
try {
return (bool) $this->connection
->query('SELECT 1 FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key AND [expire] > :now', [
':collection' => $this->collection,
':key' => $key,
':now' => REQUEST_TIME,
])
->fetchField();
} catch (\Exception $e) {
$this
->catchException($e);
return FALSE;
}
}
public function getMultiple(array $keys) {
try {
$values = $this->connection
->query('SELECT [name], [value] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [expire] > :now AND [name] IN ( :keys[] ) AND [collection] = :collection', [
':now' => REQUEST_TIME,
':keys[]' => $keys,
':collection' => $this->collection,
])
->fetchAllKeyed();
return array_map([
$this->serializer,
'decode',
], $values);
} catch (\Exception $e) {
$this
->catchException($e);
}
return [];
}
public function getAll() {
try {
$values = $this->connection
->query('SELECT [name], [value] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [expire] > :now', [
':collection' => $this->collection,
':now' => REQUEST_TIME,
])
->fetchAllKeyed();
return array_map([
$this->serializer,
'decode',
], $values);
} catch (\Exception $e) {
$this
->catchException($e);
}
return [];
}
protected function doSetWithExpire($key, $value, $expire) {
$this->connection
->merge($this->table)
->keys([
'name' => $key,
'collection' => $this->collection,
])
->fields([
'value' => $this->serializer
->encode($value),
'expire' => REQUEST_TIME + $expire,
])
->execute();
}
public function setWithExpire($key, $value, $expire) {
try {
$this
->doSetWithExpire($key, $value, $expire);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
$this
->doSetWithExpire($key, $value, $expire);
}
else {
throw $e;
}
}
}
protected function doSetWithExpireIfNotExists($key, $value, $expire) {
if (!$this
->has($key)) {
$this
->setWithExpire($key, $value, $expire);
return TRUE;
}
return FALSE;
}
public function setWithExpireIfNotExists($key, $value, $expire) {
try {
return $this
->doSetWithExpireIfNotExists($key, $value, $expire);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
return $this
->doSetWithExpireIfNotExists($key, $value, $expire);
}
else {
throw $e;
}
}
}
public function setMultipleWithExpire(array $data, $expire) {
foreach ($data as $key => $value) {
$this
->setWithExpire($key, $value, $expire);
}
}
public function deleteMultiple(array $keys) {
parent::deleteMultiple($keys);
}
public static function schemaDefinition() {
return [
'description' => 'Generic key/value storage table with an expiration.',
'fields' => [
'collection' => [
'description' => 'A named collection of key and value pairs.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'The key of the key/value pair.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'The value of the key/value pair.',
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
],
'expire' => [
'description' => 'The time since Unix epoch in seconds when this item expires. Defaults to the maximum possible time.',
'type' => 'int',
'not null' => TRUE,
'default' => 2147483647,
],
],
'primary key' => [
'collection',
'name',
],
'indexes' => [
'expire' => [
'expire',
],
],
];
}
}