class KeyValueDatabaseExpirableFactory

Same name in other branches
  1. 9 core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory
  2. 8.9.x core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory
  3. 11.x core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory

Defines the key/value store factory for the database backend.

Hierarchy

Expanded class hierarchy of KeyValueDatabaseExpirableFactory

1 file declares its use of KeyValueDatabaseExpirableFactory
system.module in core/modules/system/system.module
Configuration system that lets administrators modify the workings of the site.
1 string reference to 'KeyValueDatabaseExpirableFactory'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses KeyValueDatabaseExpirableFactory
keyvalue.expirable.database in core/core.services.yml
Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory

File

core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php, line 12

Namespace

Drupal\Core\KeyValueStore
View source
class KeyValueDatabaseExpirableFactory implements KeyValueExpirableFactoryInterface {
    
    /**
     * Holds references to each instantiation so they can be terminated.
     *
     * @var \Drupal\Core\KeyValueStore\DatabaseStorageExpirable[]
     */
    protected $storages = [];
    
    /**
     * The serialization class to use.
     *
     * @var \Drupal\Component\Serialization\SerializationInterface
     */
    protected $serializer;
    
    /**
     * The database connection.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $connection;
    
    /**
     * Constructs this factory object.
     *
     * @param \Drupal\Component\Serialization\SerializationInterface $serializer
     *   The serialization class to use.
     * @param \Drupal\Core\Database\Connection $connection
     *   The Connection object containing the key-value tables.
     * @param \Drupal\Component\Datetime\TimeInterface|null $time
     *   The time service.
     */
    public function __construct(SerializationInterface $serializer, Connection $connection, ?TimeInterface $time = NULL) {
        $this->serializer = $serializer;
        $this->connection = $connection;
        if (!$time) {
            @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3387233', E_USER_DEPRECATED);
            $this->time = \Drupal::service(TimeInterface::class);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function get($collection) {
        if (!isset($this->storages[$collection])) {
            $this->storages[$collection] = new DatabaseStorageExpirable($collection, $this->serializer, $this->connection, $this->time);
        }
        return $this->storages[$collection];
    }
    
    /**
     * Deletes expired items.
     */
    public function garbageCollection() {
        try {
            $this->connection
                ->delete('key_value_expire')
                ->condition('expire', $this->time
                ->getRequestTime(), '<')
                ->execute();
        } catch (\Exception $e) {
            $this->catchException($e);
        }
    }
    
    /**
     * Act on an exception when the table might not have been created.
     *
     * If the table does not yet exist, that's fine, but if the table exists and
     * yet the query failed, then the exception needs to propagate.
     *
     * @param \Exception $e
     *   The exception.
     *
     * @throws \Exception
     */
    protected function catchException(\Exception $e) {
        if ($this->connection
            ->schema()
            ->tableExists('key_value_expire')) {
            throw $e;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
KeyValueDatabaseExpirableFactory::$connection protected property The database connection.
KeyValueDatabaseExpirableFactory::$serializer protected property The serialization class to use.
KeyValueDatabaseExpirableFactory::$storages protected property Holds references to each instantiation so they can be terminated.
KeyValueDatabaseExpirableFactory::catchException protected function Act on an exception when the table might not have been created.
KeyValueDatabaseExpirableFactory::garbageCollection public function Deletes expired items.
KeyValueDatabaseExpirableFactory::get public function Constructs a new expirable key/value store for a given collection name. Overrides KeyValueExpirableFactoryInterface::get
KeyValueDatabaseExpirableFactory::__construct public function Constructs this factory object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.