KeyValueDatabaseExpirableFactory.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\Core\KeyValueStore;
  3. use Drupal\Component\Serialization\SerializationInterface;
  4. use Drupal\Core\Database\Connection;
  5. /**
  6. * Defines the key/value store factory for the database backend.
  7. */
  8. class KeyValueDatabaseExpirableFactory implements KeyValueExpirableFactoryInterface {
  9. /**
  10. * Holds references to each instantiation so they can be terminated.
  11. *
  12. * @var \Drupal\Core\KeyValueStore\DatabaseStorageExpirable[]
  13. */
  14. protected $storages = [];
  15. /**
  16. * The serialization class to use.
  17. *
  18. * @var \Drupal\Component\Serialization\SerializationInterface
  19. */
  20. protected $serializer;
  21. /**
  22. * The database connection.
  23. *
  24. * @var \Drupal\Core\Database\Connection
  25. */
  26. protected $connection;
  27. /**
  28. * Constructs this factory object.
  29. *
  30. * @param \Drupal\Component\Serialization\SerializationInterface $serializer
  31. * The serialization class to use.
  32. * @param \Drupal\Core\Database\Connection $connection
  33. * The Connection object containing the key-value tables.
  34. */
  35. public function __construct(SerializationInterface $serializer, Connection $connection) {
  36. $this->serializer = $serializer;
  37. $this->connection = $connection;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function get($collection) {
  43. if (!isset($this->storages[$collection])) {
  44. $this->storages[$collection] = new DatabaseStorageExpirable($collection, $this->serializer, $this->connection);
  45. }
  46. return $this->storages[$collection];
  47. }
  48. /**
  49. * Deletes expired items.
  50. */
  51. public function garbageCollection() {
  52. $this->connection->delete('key_value_expire')
  53. ->condition('expire', REQUEST_TIME, '<')
  54. ->execute();
  55. }
  56. }