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