CachedStorage.php

Contains Drupal\Core\Config\CachedStorage.

Namespace

Drupal\Core\Config

File

drupal/core/lib/Drupal/Core/Config/CachedStorage.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains Drupal\Core\Config\CachedStorage.
  5. */
  6. namespace Drupal\Core\Config;
  7. use Drupal\Core\Cache\CacheBackendInterface;
  8. /**
  9. * Defines the cached storage controller.
  10. *
  11. * The class gets another storage and a cache backend injected. It reads from
  12. * the cache and delegates the read to the storage on a cache miss. It also
  13. * handles cache invalidation.
  14. */
  15. class CachedStorage implements StorageInterface {
  16. /**
  17. * The configuration storage to be cached.
  18. *
  19. * @var Drupal\Core\Config\StorageInterface
  20. */
  21. protected $storage;
  22. /**
  23. * The instantiated Cache backend.
  24. *
  25. * @var Drupal\Core\Cache\CacheBackendInterface
  26. */
  27. protected $cache;
  28. /**
  29. * Constructs a new CachedStorage controller.
  30. *
  31. * @param Drupal\Core\Config\StorageInterface $storage
  32. * A configuration storage controller to be cached.
  33. * @param Drupal\Core\Cache\CacheBackendInterface $cache
  34. * A cache backend instance to use for caching.
  35. */
  36. public function __construct(StorageInterface $storage, CacheBackendInterface $cache) {
  37. $this->storage = $storage;
  38. $this->cache = $cache;
  39. }
  40. /**
  41. * Implements Drupal\Core\Config\StorageInterface::exists().
  42. */
  43. public function exists($name) {
  44. // The cache would read in the entire data (instead of only checking whether
  45. // any data exists), and on a potential cache miss, an additional storage
  46. // lookup would have to happen, so check the storage directly.
  47. return $this->storage->exists($name);
  48. }
  49. /**
  50. * Implements Drupal\Core\Config\StorageInterface::read().
  51. */
  52. public function read($name) {
  53. if ($cache = $this->cache->get($name)) {
  54. // The cache backend supports primitive data types, but only an array
  55. // represents valid config object data.
  56. if (is_array($cache->data)) {
  57. return $cache->data;
  58. }
  59. }
  60. // Read from the storage on a cache miss and cache the data, if any.
  61. $data = $this->storage->read($name);
  62. if ($data !== FALSE) {
  63. $this->cache->set($name, $data, CacheBackendInterface::CACHE_PERMANENT);
  64. }
  65. // If the cache contained bogus data and there is no data in the storage,
  66. // wipe the cache entry.
  67. elseif ($cache) {
  68. $this->cache->delete($name);
  69. }
  70. return $data;
  71. }
  72. /**
  73. * Implements Drupal\Core\Config\StorageInterface::write().
  74. */
  75. public function write($name, array $data) {
  76. if ($this->storage->write($name, $data)) {
  77. // While not all written data is read back, setting the cache instead of
  78. // just deleting it avoids cache rebuild stampedes.
  79. $this->cache->set($name, $data, CacheBackendInterface::CACHE_PERMANENT);
  80. return TRUE;
  81. }
  82. return FALSE;
  83. }
  84. /**
  85. * Implements Drupal\Core\Config\StorageInterface::delete().
  86. */
  87. public function delete($name) {
  88. // If the cache was the first to be deleted, another process might start
  89. // rebuilding the cache before the storage is gone.
  90. if ($this->storage->delete($name)) {
  91. $this->cache->delete($name);
  92. return TRUE;
  93. }
  94. return FALSE;
  95. }
  96. /**
  97. * Implements Drupal\Core\Config\StorageInterface::rename().
  98. */
  99. public function rename($name, $new_name) {
  100. // If the cache was the first to be deleted, another process might start
  101. // rebuilding the cache before the storage is renamed.
  102. if ($this->storage->rename($name, $new_name)) {
  103. $this->cache->delete($name);
  104. $this->cache->delete($new_name);
  105. return TRUE;
  106. }
  107. return FALSE;
  108. }
  109. /**
  110. * Implements Drupal\Core\Config\StorageInterface::encode().
  111. */
  112. public function encode($data) {
  113. return $this->storage->encode($data);
  114. }
  115. /**
  116. * Implements Drupal\Core\Config\StorageInterface::decode().
  117. */
  118. public function decode($raw) {
  119. return $this->storage->decode($raw);
  120. }
  121. /**
  122. * Implements Drupal\Core\Config\StorageInterface::listAll().
  123. *
  124. * Not supported by CacheBackendInterface.
  125. */
  126. public function listAll($prefix = '') {
  127. return $this->storage->listAll($prefix);
  128. }
  129. /**
  130. * Implements Drupal\Core\Config\StorageInterface::deleteAll().
  131. */
  132. public function deleteAll($prefix = '') {
  133. // If the cache was the first to be deleted, another process might start
  134. // rebuilding the cache before the storage is renamed.
  135. $cids = $this->storage->listAll($prefix);
  136. if ($this->storage->deleteAll($prefix)) {
  137. $this->cache->deleteMultiple($cids);
  138. return TRUE;
  139. }
  140. return FALSE;
  141. }
  142. }

Classes

Namesort descending Description
CachedStorage Defines the cached storage controller.