BackendChain.php

Definition of Drupal\Core\Cache\BackendChain.

Namespace

Drupal\Core\Cache

File

drupal/core/lib/Drupal/Core/Cache/BackendChain.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Core\Cache\BackendChain.
  5. */
  6. namespace Drupal\Core\Cache;
  7. /**
  8. * Defines a chained cache implementation for combining multiple cache backends.
  9. *
  10. * Can be used to combine two or more backends together to behave as if they
  11. * were a single backend.
  12. *
  13. * For example a slower, persistent storage engine could be combined with a
  14. * faster, volatile storage engine. When retrieving items from cache, they will
  15. * be fetched from the volatile backend first, only falling back to the
  16. * persistent backend if an item is not available. An item not present in the
  17. * volatile backend but found in the persistent one will be propagated back up
  18. * to ensure fast retrieval on the next request. On cache sets and deletes, both
  19. * backends will be invoked to ensure consistency.
  20. */
  21. class BackendChain implements CacheBackendInterface {
  22. /**
  23. * Ordered list of CacheBackendInterface instances.
  24. *
  25. * @var array
  26. */
  27. protected $backends = array();
  28. /**
  29. * Constructs a DatabaseBackend object.
  30. *
  31. * @param string $bin
  32. * The cache bin for which the object is created.
  33. */
  34. public function __construct($bin) {
  35. }
  36. /**
  37. * Appends a cache backend to the cache chain.
  38. *
  39. * @param CacheBackendInterface $backend
  40. * The cache backend to be appended to the cache chain.
  41. *
  42. * @return Drupal\Core\Cache\BackendChain
  43. * The called object.
  44. */
  45. public function appendBackend(CacheBackendInterface $backend) {
  46. $this->backends[] = $backend;
  47. return $this;
  48. }
  49. /**
  50. * Prepends a cache backend to the cache chain.
  51. *
  52. * @param CacheBackendInterface $backend
  53. * The backend to be prepended to the cache chain.
  54. *
  55. * @return Drupal\Core\Cache\BackendChain
  56. * The called object.
  57. */
  58. public function prependBackend(CacheBackendInterface $backend) {
  59. array_unshift($this->backends, $backend);
  60. return $this;
  61. }
  62. /**
  63. * Implements Drupal\Core\Cache\CacheBackendInterface::get().
  64. */
  65. public function get($cid, $allow_invalid = FALSE) {
  66. foreach ($this->backends as $index => $backend) {
  67. if (($return = $backend->get($cid, $allow_invalid)) !== FALSE) {
  68. // We found a result, propagate it to all missed backends.
  69. if ($index > 0) {
  70. for ($i = ($index - 1); 0 <= $i; --$i) {
  71. $this->backends[$i]->set($cid, $return->data, $return->expire, $return->tags);
  72. }
  73. }
  74. return $return;
  75. }
  76. }
  77. return FALSE;
  78. }
  79. /**
  80. * Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple().
  81. */
  82. public function getMultiple(&$cids, $allow_invalid = FALSE) {
  83. $return = array();
  84. foreach ($this->backends as $index => $backend) {
  85. $items = $backend->getMultiple($cids, $allow_invalid);
  86. // Propagate the values that could be retrieved from the current cache
  87. // backend to all missed backends.
  88. if ($index > 0 && !empty($items)) {
  89. for ($i = ($index - 1); 0 <= $i; --$i) {
  90. foreach ($items as $cached) {
  91. $this->backends[$i]->set($cached->cid, $cached->data, $cached->expire, $cached->tags);
  92. }
  93. }
  94. }
  95. // Append the values to the previously retrieved ones.
  96. $return += $items;
  97. if (empty($cids)) {
  98. // No need to go further if we don't have any cid to fetch left.
  99. break;
  100. }
  101. }
  102. return $return;
  103. }
  104. /**
  105. * Implements Drupal\Core\Cache\CacheBackendInterface::set().
  106. */
  107. public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array()) {
  108. foreach ($this->backends as $backend) {
  109. $backend->set($cid, $data, $expire, $tags);
  110. }
  111. }
  112. /**
  113. * Implements Drupal\Core\Cache\CacheBackendInterface::delete().
  114. */
  115. public function delete($cid) {
  116. foreach ($this->backends as $backend) {
  117. $backend->delete($cid);
  118. }
  119. }
  120. /**
  121. * Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
  122. */
  123. public function deleteMultiple(array $cids) {
  124. foreach ($this->backends as $backend) {
  125. $backend->deleteMultiple($cids);
  126. }
  127. }
  128. /**
  129. * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags().
  130. */
  131. public function deleteTags(array $tags) {
  132. foreach ($this->backends as $backend) {
  133. $backend->deleteTags($tags);
  134. }
  135. }
  136. /**
  137. * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll().
  138. */
  139. public function deleteAll() {
  140. foreach ($this->backends as $backend) {
  141. $backend->deleteAll();
  142. }
  143. }
  144. /**
  145. * Implements Drupal\Core\Cache\CacheBackendInterface::invalidate().
  146. */
  147. public function invalidate($cid) {
  148. foreach ($this->backends as $backend) {
  149. $backend->invalidate($cid);
  150. }
  151. }
  152. /**
  153. * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
  154. */
  155. public function invalidateMultiple(array $cids) {
  156. foreach ($this->backends as $backend) {
  157. $backend->invalidateMultiple($cids);
  158. }
  159. }
  160. /**
  161. * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
  162. */
  163. public function invalidateTags(array $tags) {
  164. foreach ($this->backends as $backend) {
  165. $backend->invalidateTags($tags);
  166. }
  167. }
  168. /**
  169. * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
  170. */
  171. public function invalidateAll() {
  172. foreach ($this->backends as $backend) {
  173. $backend->invalidateAll();
  174. }
  175. }
  176. /**
  177. * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection().
  178. */
  179. public function garbageCollection() {
  180. foreach ($this->backends as $backend) {
  181. $backend->garbageCollection();
  182. }
  183. }
  184. /**
  185. * Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty().
  186. */
  187. public function isEmpty() {
  188. foreach ($this->backends as $backend) {
  189. if (!$backend->isEmpty()) {
  190. return FALSE;
  191. }
  192. }
  193. return TRUE;
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function removeBin() {
  199. foreach ($this->backends as $backend) {
  200. $this->removeBin();
  201. }
  202. }
  203. }

Classes

Namesort descending Description
BackendChain