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