comparison core/lib/Drupal/Core/Cache/BackendChain.php @ 0:4c8ae668cc8c

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