class DatabaseStorage extends StorageBase (View source)

Defines a default key/value store implementation.

This is Drupal's default key/value store implementation. It uses the database to store key/value data.

Traits

Provides dependency injection friendly methods for serialization.

Properties

protected string $collection

The name of the collection holding key and value pairs.

from  StorageBase
protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected SerializationInterface $serializer

The serialization class to use.

protected Connection $connection

The database connection.

protected string $table

The name of the SQL table to use.

Methods

__construct($collection, SerializationInterface $serializer, Connection $connection, string $table = 'key_value')

Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().

string
getCollectionName()

Returns the name of this collection.

mixed
get(string $key, mixed $default = NULL)

Returns the stored value for a given key.

setMultiple(array $data)

Saves key/value pairs.

delete(string $key)

Deletes an item from the key/value store.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

bool
has(string $key)

Returns whether a given key exists in the store.

array
getMultiple(array $keys)

Returns the stored key/value pairs for a given set of keys.

array
getAll()

Returns all stored key/value pairs in the collection.

doSet(string $key, mixed $value)

Saves a value for a given key.

set(string $key, mixed $value)

Saves a value for a given key.

bool
doSetIfNotExists(string $key, mixed $value)

Saves a value for a given key if it does not exist yet.

bool
setIfNotExists(string $key, mixed $value)

Saves a value for a given key if it does not exist yet.

rename(string $key, string $new_key)

Renames a key.

deleteMultiple(array $keys)

Deletes multiple items from the key/value store.

deleteAll()

Deletes all items from the key/value store.

bool
ensureTableExists()

Check if the table exists and create it if not.

catchException(Exception $e)

Act on an exception when the table might not have been created.

static 
schemaDefinition()

Defines the schema for the key_value table.

Details

__construct($collection, SerializationInterface $serializer, Connection $connection, string $table = 'key_value')

Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().

Parameters

$collection
SerializationInterface $serializer

The serialization class to use.

Connection $connection

The database connection to use.

string $table

The name of the SQL table to use, defaults to key_value.

string getCollectionName()

Returns the name of this collection.

Return Value

string

The name of this collection.

mixed get(string $key, mixed $default = NULL)

Returns the stored value for a given key.

Parameters

string $key

The key of the data to retrieve.

mixed $default

The default value to use if the key is not found.

Return Value

mixed

The stored value, or the default value if no value exists.

setMultiple(array $data)

Saves key/value pairs.

Parameters

array $data

An associative array of key/value pairs.

delete(string $key)

Deletes an item from the key/value store.

Parameters

string $key

The item name to delete.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

bool has(string $key)

Returns whether a given key exists in the store.

Parameters

string $key

The key to check.

Return Value

bool

TRUE if the key exists, FALSE otherwise.

array getMultiple(array $keys)

Returns the stored key/value pairs for a given set of keys.

Parameters

array $keys

A list of keys to retrieve.

Return Value

array

An associative array of items successfully returned, indexed by key.

array getAll()

Returns all stored key/value pairs in the collection.

Return Value

array

An associative array containing all stored items in the collection.

protected doSet(string $key, mixed $value)

Saves a value for a given key.

This will be called by set() within a try block.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

set(string $key, mixed $value)

Saves a value for a given key.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

bool doSetIfNotExists(string $key, mixed $value)

Saves a value for a given key if it does not exist yet.

This will be called by setIfNotExists() within a try block.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

Return Value

bool

TRUE if the data was set, FALSE if it already existed.

bool setIfNotExists(string $key, mixed $value)

Saves a value for a given key if it does not exist yet.

Parameters

string $key

The key of the data to store.

mixed $value

The data to store.

Return Value

bool

TRUE if the data was set, FALSE if it already existed.

rename(string $key, string $new_key)

Renames a key.

Parameters

string $key

The key to rename.

string $new_key

The new key name.

deleteMultiple(array $keys)

Deletes multiple items from the key/value store.

Parameters

array $keys

A list of item names to delete.

deleteAll()

Deletes all items from the key/value store.

protected bool ensureTableExists()

Check if the table exists and create it if not.

Return Value

bool

TRUE if the table exists, FALSE if it does not exists.

protected catchException(Exception $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 if it is not a DatabaseException. Due to race conditions it is possible that another request has created the table in the meantime. Therefore we can not rethrow for any database exception.

Parameters

Exception $e

The exception.

Exceptions

Exception

static schemaDefinition()

Defines the schema for the key_value table.