DatabaseStorageExpirable.php 3.2 KB

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