Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Cache;
|
Chris@0
|
4
|
Chris@14
|
5 use Drupal\Component\Assertion\Inspector;
|
Chris@0
|
6 use Drupal\Core\PhpStorage\PhpStorageFactory;
|
Chris@0
|
7 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Defines a PHP cache implementation.
|
Chris@0
|
11 *
|
Chris@0
|
12 * Stores cache items in a PHP file using a storage that implements
|
Chris@0
|
13 * Drupal\Component\PhpStorage\PhpStorageInterface.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This is fast because of PHP's opcode caching mechanism. Once a file's
|
Chris@0
|
16 * content is stored in PHP's opcode cache, including it doesn't require
|
Chris@0
|
17 * reading the contents from a filesystem. Instead, PHP will use the already
|
Chris@0
|
18 * compiled opcodes stored in memory.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @ingroup cache
|
Chris@0
|
21 */
|
Chris@0
|
22 class PhpBackend implements CacheBackendInterface {
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * @var string
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $bin;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Array to store cache objects.
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $cache = [];
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The cache tags checksum provider.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $checksumProvider;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Constructs a PhpBackend object.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param string $bin
|
Chris@0
|
45 * The cache bin for which the object is created.
|
Chris@0
|
46 * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
|
Chris@0
|
47 * The cache tags checksum provider.
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __construct($bin, CacheTagsChecksumInterface $checksum_provider) {
|
Chris@0
|
50 $this->bin = 'cache_' . $bin;
|
Chris@0
|
51 $this->checksumProvider = $checksum_provider;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public function get($cid, $allow_invalid = FALSE) {
|
Chris@0
|
58 return $this->getByHash($this->normalizeCid($cid), $allow_invalid);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Fetch a cache item using a hashed cache ID.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @param string $cidhash
|
Chris@0
|
65 * The hashed version of the original cache ID after being normalized.
|
Chris@0
|
66 * @param bool $allow_invalid
|
Chris@0
|
67 * (optional) If TRUE, a cache item may be returned even if it is expired or
|
Chris@0
|
68 * has been invalidated.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return bool|mixed
|
Chris@0
|
71 */
|
Chris@0
|
72 protected function getByHash($cidhash, $allow_invalid = FALSE) {
|
Chris@0
|
73 if ($file = $this->storage()->getFullPath($cidhash)) {
|
Chris@0
|
74 $cache = @include $file;
|
Chris@0
|
75 }
|
Chris@0
|
76 if (isset($cache)) {
|
Chris@0
|
77 return $this->prepareItem($cache, $allow_invalid);
|
Chris@0
|
78 }
|
Chris@0
|
79 return FALSE;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function setMultiple(array $items) {
|
Chris@0
|
86 foreach ($items as $cid => $item) {
|
Chris@0
|
87 $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * {@inheritdoc}
|
Chris@0
|
93 */
|
Chris@0
|
94 public function getMultiple(&$cids, $allow_invalid = FALSE) {
|
Chris@0
|
95 $ret = [];
|
Chris@0
|
96
|
Chris@0
|
97 foreach ($cids as $cid) {
|
Chris@0
|
98 if ($item = $this->get($cid, $allow_invalid)) {
|
Chris@0
|
99 $ret[$item->cid] = $item;
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 $cids = array_diff($cids, array_keys($ret));
|
Chris@0
|
104
|
Chris@0
|
105 return $ret;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Prepares a cached item.
|
Chris@0
|
110 *
|
Chris@0
|
111 * Checks that items are either permanent or did not expire, and returns data
|
Chris@0
|
112 * as appropriate.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param object $cache
|
Chris@18
|
115 * An item loaded from self::get() or self::getMultiple().
|
Chris@0
|
116 * @param bool $allow_invalid
|
Chris@0
|
117 * If FALSE, the method returns FALSE if the cache item is not valid.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @return mixed
|
Chris@0
|
120 * The item with data as appropriate or FALSE if there is no
|
Chris@0
|
121 * valid item to load.
|
Chris@0
|
122 */
|
Chris@0
|
123 protected function prepareItem($cache, $allow_invalid) {
|
Chris@0
|
124 if (!isset($cache->data)) {
|
Chris@0
|
125 return FALSE;
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 // Check expire time.
|
Chris@0
|
129 $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
|
Chris@0
|
130
|
Chris@0
|
131 // Check if invalidateTags() has been called with any of the item's tags.
|
Chris@0
|
132 if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
|
Chris@0
|
133 $cache->valid = FALSE;
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 if (!$allow_invalid && !$cache->valid) {
|
Chris@0
|
137 return FALSE;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 return $cache;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
|
Chris@14
|
147 assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
|
Chris@14
|
148
|
Chris@0
|
149 $item = (object) [
|
Chris@0
|
150 'cid' => $cid,
|
Chris@0
|
151 'data' => $data,
|
Chris@0
|
152 'created' => round(microtime(TRUE), 3),
|
Chris@0
|
153 'expire' => $expire,
|
Chris@0
|
154 'tags' => array_unique($tags),
|
Chris@0
|
155 'checksum' => $this->checksumProvider->getCurrentChecksum($tags),
|
Chris@0
|
156 ];
|
Chris@0
|
157 $this->writeItem($this->normalizeCid($cid), $item);
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * {@inheritdoc}
|
Chris@0
|
162 */
|
Chris@0
|
163 public function delete($cid) {
|
Chris@0
|
164 $this->storage()->delete($this->normalizeCid($cid));
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * {@inheritdoc}
|
Chris@0
|
169 */
|
Chris@0
|
170 public function deleteMultiple(array $cids) {
|
Chris@0
|
171 foreach ($cids as $cid) {
|
Chris@0
|
172 $this->delete($cid);
|
Chris@0
|
173 }
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * {@inheritdoc}
|
Chris@0
|
178 */
|
Chris@0
|
179 public function deleteAll() {
|
Chris@0
|
180 $this->storage()->deleteAll();
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * {@inheritdoc}
|
Chris@0
|
185 */
|
Chris@0
|
186 public function invalidate($cid) {
|
Chris@0
|
187 $this->invalidatebyHash($this->normalizeCid($cid));
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 /**
|
Chris@0
|
191 * Invalidate one cache item.
|
Chris@0
|
192 *
|
Chris@0
|
193 * @param string $cidhash
|
Chris@0
|
194 * The hashed version of the original cache ID after being normalized.
|
Chris@0
|
195 */
|
Chris@0
|
196 protected function invalidatebyHash($cidhash) {
|
Chris@0
|
197 if ($item = $this->getByHash($cidhash)) {
|
Chris@0
|
198 $item->expire = REQUEST_TIME - 1;
|
Chris@0
|
199 $this->writeItem($cidhash, $item);
|
Chris@0
|
200 }
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * {@inheritdoc}
|
Chris@0
|
205 */
|
Chris@0
|
206 public function invalidateMultiple(array $cids) {
|
Chris@0
|
207 foreach ($cids as $cid) {
|
Chris@0
|
208 $this->invalidate($cid);
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * {@inheritdoc}
|
Chris@0
|
214 */
|
Chris@0
|
215 public function invalidateAll() {
|
Chris@0
|
216 foreach ($this->storage()->listAll() as $cidhash) {
|
Chris@0
|
217 $this->invalidatebyHash($cidhash);
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * {@inheritdoc}
|
Chris@0
|
223 */
|
Chris@0
|
224 public function garbageCollection() {
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 /**
|
Chris@0
|
228 * {@inheritdoc}
|
Chris@0
|
229 */
|
Chris@0
|
230 public function removeBin() {
|
Chris@0
|
231 $this->cache = [];
|
Chris@0
|
232 $this->storage()->deleteAll();
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * Writes a cache item to PhpStorage.
|
Chris@0
|
237 *
|
Chris@0
|
238 * @param string $cidhash
|
Chris@0
|
239 * The hashed version of the original cache ID after being normalized.
|
Chris@0
|
240 * @param \stdClass $item
|
Chris@0
|
241 * The cache item to store.
|
Chris@0
|
242 */
|
Chris@0
|
243 protected function writeItem($cidhash, \stdClass $item) {
|
Chris@0
|
244 $content = '<?php return unserialize(' . var_export(serialize($item), TRUE) . ');';
|
Chris@0
|
245 $this->storage()->save($cidhash, $content);
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Gets the PHP code storage object to use.
|
Chris@0
|
250 *
|
Chris@0
|
251 * @return \Drupal\Component\PhpStorage\PhpStorageInterface
|
Chris@0
|
252 */
|
Chris@0
|
253 protected function storage() {
|
Chris@0
|
254 if (!isset($this->storage)) {
|
Chris@0
|
255 $this->storage = PhpStorageFactory::get($this->bin);
|
Chris@0
|
256 }
|
Chris@0
|
257 return $this->storage;
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Ensures a normalized cache ID.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @param string $cid
|
Chris@0
|
264 * The passed in cache ID.
|
Chris@0
|
265 *
|
Chris@0
|
266 * @return string
|
Chris@0
|
267 * A normalized cache ID.
|
Chris@0
|
268 */
|
Chris@0
|
269 protected function normalizeCid($cid) {
|
Chris@0
|
270 return Crypt::hashBase64($cid);
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 }
|