class Storage in Express 8
Theme Storage.
A hybrid storage solution that utilizes the cache system for complex and expensive operations performed by a [base] theme.
Instead of using multiple cache identifiers, which increases the number of database calls, this storage only executes a single cache call and stores individual entries in memory as an associative array.
It also tracks when the data has been modified so it can be saved back to cache before the system fully shuts down.
This storage object can be used in `foreach` loops.
Hierarchy
- class \Drupal\Core\KeyValueStore\StorageBase implements KeyValueStoreInterface
- class \Drupal\Core\KeyValueStore\MemoryStorage
- class \Drupal\bootstrap\Utility\Storage implements \Drupal\bootstrap\Utility\Iterator
- class \Drupal\Core\KeyValueStore\MemoryStorage
Expanded class hierarchy of Storage
See also
\Drupal\bootstrap\Utility\StorageItem
1 file declares its use of Storage
File
- themes/
contrib/ bootstrap/ src/ Utility/ Storage.php, line 32 - Contains \Drupal\bootstrap\Utility\Storage.
Namespace
Drupal\bootstrap\UtilityView source
class Storage extends MemoryStorage implements \Iterator {
/**
* The bin (table) data should be stored in (not prefixed with "cache_").
*
* @var string
*/
protected $bin;
/**
* Flag determining whether or not the cache should be saved to the database.
*
* @var bool
*/
protected $changed;
/**
* The cache identifier.
*
* @var string
*/
protected $cid;
/**
* Indicates when the cache should expire.
*
* @var string
*/
protected $expire;
/**
* Flag determining whether or not object has been initialized yet.
*
* @var bool
*/
protected $initialized = FALSE;
/**
* Tags to associate with the cached data so it can be properly invalidated.
*
* @var string
*/
protected $tags;
/**
* {@inheritdoc}
*/
public function __construct($cid, $bin = 'default', $expire = Cache::PERMANENT, $tags = [
Bootstrap::CACHE_TAG,
]) {
$this->cid = "theme_registry:storage:{$cid}";
$this->bin = $bin;
$this->changed = FALSE;
$this->expire = $expire;
$this->tags = $tags;
// Register the cache object to save, if it's needed, on shutdown.
drupal_register_shutdown_function([
$this,
'save',
]);
// Retrieve the cached data.
$data = ($cached = \Drupal::cache($bin)
->get($this->cid)) && !empty($cached->data) ? $cached->data : [];
// Set the data.
$this
->setMultiple($data);
// Cache has been initialized.
$this->initialized = TRUE;
}
/**
* Notifies the object that data has changed.
*/
public function changed() {
if ($this->initialized) {
$this->changed = TRUE;
}
}
/**
* {@inheritdoc}
*/
public function current() {
return current($this->data);
}
/**
* {@inheritdoc}
*/
public function delete($key) {
parent::delete($key);
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function deleteAll() {
parent::deleteAll();
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function deleteMultiple(array $keys) {
parent::deleteMultiple($keys);
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function getAll($arrays = TRUE) {
$data = $this->data;
if ($arrays) {
foreach ($data as $key => $value) {
if ($value instanceof StorageItem) {
$data[$key] = $value
->getAll();
}
}
}
return $data;
}
/**
* Determines if the cache is empty.
*
* @return bool
* TRUE or FALSE
*/
public function isEmpty() {
return empty($this->data);
}
/**
* {@inheritdoc}
*/
public function key() {
return key($this->data);
}
/**
* {@inheritdoc}
*/
public function next() {
return next($this->data);
}
/**
* {@inheritdoc}
*/
public function rename($key, $new_key) {
parent::rename($key, $new_key);
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function rewind() {
return reset($this->data);
}
/**
* Saves the data back to the database, if necessary, on shutdown.
*
* This method is automatically invoked during PHP shutdown.
*
* @internal
*
* @see \Drupal\bootstrap\Utility\Storage::__construct
*/
public function save() {
if ($this->changed) {
\Drupal::cache($this->bin)
->set($this->cid, $this
->getAll(), $this->expire, $this->tags);
}
}
/**
* {@inheritdoc}
*/
public function set($key, $value) {
if (is_array($value)) {
$value = new StorageItem($value, $this);
}
parent::set($key, $value);
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function setIfNotExists($key, $value) {
if (!isset($this->data[$key])) {
if (is_array($value)) {
$value = new StorageItem($value, $this);
}
$this->data[$key] = $value;
$this
->changed();
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function setMultiple(array $data) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = new StorageItem($value, $this);
}
}
parent::setMultiple($data);
$this
->changed();
}
/**
* {@inheritdoc}
*/
public function valid() {
return key($this->data) !== NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MemoryStorage:: |
protected | property | The actual storage of key-value pairs. | |
MemoryStorage:: |
public | function |
Returns the stored value for a given key. Overrides StorageBase:: |
|
MemoryStorage:: |
public | function |
Returns the stored key/value pairs for a given set of keys. Overrides KeyValueStoreInterface:: |
|
MemoryStorage:: |
public | function |
Returns whether a given key exists in the store. Overrides KeyValueStoreInterface:: |
|
Storage:: |
protected | property | The bin (table) data should be stored in (not prefixed with "cache_"). | |
Storage:: |
protected | property | Flag determining whether or not the cache should be saved to the database. | |
Storage:: |
protected | property | The cache identifier. | |
Storage:: |
protected | property | Indicates when the cache should expire. | |
Storage:: |
protected | property | Flag determining whether or not object has been initialized yet. | |
Storage:: |
protected | property | Tags to associate with the cached data so it can be properly invalidated. | |
Storage:: |
public | function | Notifies the object that data has changed. | |
Storage:: |
public | function | ||
Storage:: |
public | function |
Deletes an item from the key/value store. Overrides MemoryStorage:: |
|
Storage:: |
public | function |
Deletes all items from the key/value store. Overrides MemoryStorage:: |
|
Storage:: |
public | function |
Deletes multiple items from the key/value store. Overrides MemoryStorage:: |
|
Storage:: |
public | function |
Returns all stored key/value pairs in the collection. Overrides MemoryStorage:: |
|
Storage:: |
public | function | Determines if the cache is empty. | |
Storage:: |
public | function | ||
Storage:: |
public | function | ||
Storage:: |
public | function |
Renames a key. Overrides MemoryStorage:: |
|
Storage:: |
public | function | ||
Storage:: |
public | function | Saves the data back to the database, if necessary, on shutdown. | |
Storage:: |
public | function |
Saves a value for a given key. Overrides MemoryStorage:: |
|
Storage:: |
public | function |
Saves a value for a given key if it does not exist yet. Overrides MemoryStorage:: |
|
Storage:: |
public | function |
Saves key/value pairs. Overrides MemoryStorage:: |
|
Storage:: |
public | function | ||
Storage:: |
public | function |
Overrides StorageBase:: |
|
StorageBase:: |
protected | property | The name of the collection holding key and value pairs. | |
StorageBase:: |
public | function |
Returns the name of this collection. Overrides KeyValueStoreInterface:: |