3 namespace Drupal\Core\Cache;
5 use Drupal\Component\Assertion\Inspector;
8 * Defines a memory cache implementation.
10 * Stores cache items in memory using a PHP array.
12 * Should be used for unit tests and specialist use-cases only, does not
13 * store cached items between requests.
15 * The functions ::prepareItem()/::set() use unserialize()/serialize(). It
16 * behaves as an external cache backend to avoid changing the cached data by
17 * reference. In ::prepareItem(), the object is not modified by the call to
18 * unserialize() because we make a clone of it.
22 class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
25 * Array to store cache objects.
27 protected $cache = [];
32 public function get($cid, $allow_invalid = FALSE) {
33 if (isset($this->cache[$cid])) {
34 return $this->prepareItem($this->cache[$cid], $allow_invalid);
44 public function getMultiple(&$cids, $allow_invalid = FALSE) {
47 $items = array_intersect_key($this->cache, array_flip($cids));
49 foreach ($items as $item) {
50 $item = $this->prepareItem($item, $allow_invalid);
52 $ret[$item->cid] = $item;
56 $cids = array_diff($cids, array_keys($ret));
62 * Prepares a cached item.
64 * Checks that items are either permanent or did not expire, and returns data
67 * @param object $cache
68 * An item loaded from cache_get() or cache_get_multiple().
69 * @param bool $allow_invalid
70 * (optional) If TRUE, cache items may be returned even if they have expired
71 * or been invalidated.
74 * The item with data as appropriate or FALSE if there is no
77 protected function prepareItem($cache, $allow_invalid) {
78 if (!isset($cache->data)) {
81 // The object passed into this function is the one stored in $this->cache.
82 // We must clone it as part of the preparation step so that the actual
83 // cache object is not affected by the unserialize() call or other
84 // manipulations of the returned object.
86 $prepared = clone $cache;
87 $prepared->data = unserialize($prepared->data);
90 $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
92 if (!$allow_invalid && !$prepared->valid) {
102 public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
103 assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
104 $tags = array_unique($tags);
105 // Sort the cache tags so that they are stored consistently in the database.
107 $this->cache[$cid] = (object) [
109 'data' => serialize($data),
110 'created' => $this->getRequestTime(),
119 public function setMultiple(array $items = []) {
120 foreach ($items as $cid => $item) {
121 $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
128 public function delete($cid) {
129 unset($this->cache[$cid]);
135 public function deleteMultiple(array $cids) {
136 $this->cache = array_diff_key($this->cache, array_flip($cids));
142 public function deleteAll() {
149 public function invalidate($cid) {
150 if (isset($this->cache[$cid])) {
151 $this->cache[$cid]->expire = $this->getRequestTime() - 1;
158 public function invalidateMultiple(array $cids) {
159 foreach ($cids as $cid) {
160 if (isset($this->cache[$cid])) {
161 $this->cache[$cid]->expire = $this->getRequestTime() - 1;
169 public function invalidateTags(array $tags) {
170 foreach ($this->cache as $cid => $item) {
171 if (array_intersect($tags, $item->tags)) {
172 $this->cache[$cid]->expire = $this->getRequestTime() - 1;
180 public function invalidateAll() {
181 foreach ($this->cache as $cid => $item) {
182 $this->cache[$cid]->expire = $this->getRequestTime() - 1;
189 public function garbageCollection() {
195 public function removeBin() {
200 * Wrapper method for REQUEST_TIME constant.
204 protected function getRequestTime() {
205 return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
209 * Prevents data stored in memory backends from being serialized.
211 public function __sleep() {
216 * Reset statically cached variables.
218 * This is only used by tests.
220 public function reset() {