DatabaseStorageExpirable.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Drupal\Core\KeyValueStore;
  3. use Drupal\Component\Serialization\SerializationInterface;
  4. use Drupal\Core\Database\Connection;
  5. use Drupal\Core\Database\Query\Merge;
  6. /**
  7. * Defines a default key/value store implementation for expiring items.
  8. *
  9. * This key/value store implementation uses the database to store key/value
  10. * data with an expire date.
  11. */
  12. class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreExpirableInterface {
  13. /**
  14. * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
  15. *
  16. * @param string $collection
  17. * The name of the collection holding key and value pairs.
  18. * @param \Drupal\Component\Serialization\SerializationInterface $serializer
  19. * The serialization class to use.
  20. * @param \Drupal\Core\Database\Connection $connection
  21. * The database connection to use.
  22. * @param string $table
  23. * The name of the SQL table to use, defaults to key_value_expire.
  24. */
  25. public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
  26. parent::__construct($collection, $serializer, $connection, $table);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function has($key) {
  32. return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key AND expire > :now', [
  33. ':collection' => $this->collection,
  34. ':key' => $key,
  35. ':now' => REQUEST_TIME,
  36. ])->fetchField();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getMultiple(array $keys) {
  42. $values = $this->connection->query(
  43. 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE expire > :now AND name IN ( :keys[] ) AND collection = :collection',
  44. [
  45. ':now' => REQUEST_TIME,
  46. ':keys[]' => $keys,
  47. ':collection' => $this->collection,
  48. ])->fetchAllKeyed();
  49. return array_map([$this->serializer, 'decode'], $values);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getAll() {
  55. $values = $this->connection->query(
  56. 'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now',
  57. [
  58. ':collection' => $this->collection,
  59. ':now' => REQUEST_TIME
  60. ])->fetchAllKeyed();
  61. return array_map([$this->serializer, 'decode'], $values);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setWithExpire($key, $value, $expire) {
  67. $this->connection->merge($this->table)
  68. ->keys([
  69. 'name' => $key,
  70. 'collection' => $this->collection,
  71. ])
  72. ->fields([
  73. 'value' => $this->serializer->encode($value),
  74. 'expire' => REQUEST_TIME + $expire,
  75. ])
  76. ->execute();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function setWithExpireIfNotExists($key, $value, $expire) {
  82. $result = $this->connection->merge($this->table)
  83. ->insertFields([
  84. 'collection' => $this->collection,
  85. 'name' => $key,
  86. 'value' => $this->serializer->encode($value),
  87. 'expire' => REQUEST_TIME + $expire,
  88. ])
  89. ->condition('collection', $this->collection)
  90. ->condition('name', $key)
  91. ->execute();
  92. return $result == Merge::STATUS_INSERT;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function setMultipleWithExpire(array $data, $expire) {
  98. foreach ($data as $key => $value) {
  99. $this->setWithExpire($key, $value, $expire);
  100. }
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function deleteMultiple(array $keys) {
  106. parent::deleteMultiple($keys);
  107. }
  108. }