Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Cache / MemoryBackend.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 /**
6  * Defines a memory cache implementation.
7  *
8  * Stores cache items in memory using a PHP array.
9  *
10  * Should be used for unit tests and specialist use-cases only, does not
11  * store cached items between requests.
12  *
13  * The functions ::prepareItem()/::set() use unserialize()/serialize(). It
14  * behaves as an external cache backend to avoid changing the cached data by
15  * reference. In ::prepareItem(), the object is not modified by the call to
16  * unserialize() because we make a clone of it.
17  *
18  * @ingroup cache
19  */
20 class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
21
22   /**
23    * Array to store cache objects.
24    */
25   protected $cache = [];
26
27   /**
28    * {@inheritdoc}
29    */
30   public function get($cid, $allow_invalid = FALSE) {
31     if (isset($this->cache[$cid])) {
32       return $this->prepareItem($this->cache[$cid], $allow_invalid);
33     }
34     else {
35       return FALSE;
36     }
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getMultiple(&$cids, $allow_invalid = FALSE) {
43     $ret = [];
44
45     $items = array_intersect_key($this->cache, array_flip($cids));
46
47     foreach ($items as $item) {
48       $item = $this->prepareItem($item, $allow_invalid);
49       if ($item) {
50         $ret[$item->cid] = $item;
51       }
52     }
53
54     $cids = array_diff($cids, array_keys($ret));
55
56     return $ret;
57   }
58
59   /**
60    * Prepares a cached item.
61    *
62    * Checks that items are either permanent or did not expire, and returns data
63    * as appropriate.
64    *
65    * @param object $cache
66    *   An item loaded from cache_get() or cache_get_multiple().
67    * @param bool $allow_invalid
68    *   (optional) If TRUE, cache items may be returned even if they have expired
69    *   or been invalidated.
70    *
71    * @return mixed
72    *   The item with data as appropriate or FALSE if there is no
73    *   valid item to load.
74    */
75   protected function prepareItem($cache, $allow_invalid) {
76     if (!isset($cache->data)) {
77       return FALSE;
78     }
79     // The object passed into this function is the one stored in $this->cache.
80     // We must clone it as part of the preparation step so that the actual
81     // cache object is not affected by the unserialize() call or other
82     // manipulations of the returned object.
83
84     $prepared = clone $cache;
85     $prepared->data = unserialize($prepared->data);
86
87     // Check expire time.
88     $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
89
90     if (!$allow_invalid && !$prepared->valid) {
91       return FALSE;
92     }
93
94     return $prepared;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
101     assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
102     $tags = array_unique($tags);
103     // Sort the cache tags so that they are stored consistently in the database.
104     sort($tags);
105     $this->cache[$cid] = (object) [
106       'cid' => $cid,
107       'data' => serialize($data),
108       'created' => $this->getRequestTime(),
109       'expire' => $expire,
110       'tags' => $tags,
111     ];
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function setMultiple(array $items = []) {
118     foreach ($items as $cid => $item) {
119       $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
120     }
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function delete($cid) {
127     unset($this->cache[$cid]);
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function deleteMultiple(array $cids) {
134     $this->cache = array_diff_key($this->cache, array_flip($cids));
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function deleteAll() {
141     $this->cache = [];
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function invalidate($cid) {
148     if (isset($this->cache[$cid])) {
149       $this->cache[$cid]->expire = $this->getRequestTime() - 1;
150     }
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function invalidateMultiple(array $cids) {
157     foreach ($cids as $cid) {
158       if (isset($this->cache[$cid])) {
159         $this->cache[$cid]->expire = $this->getRequestTime() - 1;
160       }
161     }
162   }
163
164   /**
165    * {@inheritdoc}
166    */
167   public function invalidateTags(array $tags) {
168     foreach ($this->cache as $cid => $item) {
169       if (array_intersect($tags, $item->tags)) {
170         $this->cache[$cid]->expire = $this->getRequestTime() - 1;
171       }
172     }
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function invalidateAll() {
179     foreach ($this->cache as $cid => $item) {
180       $this->cache[$cid]->expire = $this->getRequestTime() - 1;
181     }
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function garbageCollection() {
188   }
189
190   /**
191    * {@inheritdoc}
192    */
193   public function removeBin() {
194     $this->cache = [];
195   }
196
197   /**
198    * Wrapper method for REQUEST_TIME constant.
199    *
200    * @return int
201    */
202   protected function getRequestTime() {
203     return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
204   }
205
206   /**
207    * Prevents data stored in memory backends from being serialized.
208    */
209   public function __sleep() {
210     return [];
211   }
212
213   /**
214    * Reset statically cached variables.
215    *
216    * This is only used by tests.
217    */
218   public function reset() {
219     $this->cache = [];
220   }
221
222 }