MemoryBackend.php 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace Drupal\Core\Cache;

/**
 * Defines a memory cache implementation.
 *
 * Stores cache items in memory using a PHP array.
 *
 * Should be used for unit tests and specialist use-cases only, does not
 * store cached items between requests.
 *
13
 * @ingroup cache
14
 */
15
class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
16 17 18 19

  /**
   * Array to store cache objects.
   */
20
  protected $cache = array();
21 22

  /**
23
   * {@inheritdoc}
24
   */
25
  public function get($cid, $allow_invalid = FALSE) {
26
    if (isset($this->cache[$cid])) {
27
      return $this->prepareItem($this->cache[$cid], $allow_invalid);
28 29 30 31 32 33 34
    }
    else {
      return FALSE;
    }
  }

  /**
35
   * {@inheritdoc}
36
   */
37
  public function getMultiple(&$cids, $allow_invalid = FALSE) {
38 39 40 41 42
    $ret = array();

    $items = array_intersect_key($this->cache, array_flip($cids));

    foreach ($items as $item) {
43
      $item = $this->prepareItem($item, $allow_invalid);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      if ($item) {
        $ret[$item->cid] = $item;
      }
    }

    $cids = array_diff($cids, array_keys($ret));

    return $ret;
  }

  /**
   * Prepares a cached item.
   *
   * Checks that items are either permanent or did not expire, and returns data
   * as appropriate.
   *
60
   * @param object $cache
61
   *   An item loaded from cache_get() or cache_get_multiple().
62 63 64
   * @param bool $allow_invalid
   *   (optional) If TRUE, cache items may be returned even if they have expired
   *   or been invalidated.
65 66 67 68 69
   *
   * @return mixed
   *   The item with data as appropriate or FALSE if there is no
   *   valid item to load.
   */
70
  protected function prepareItem($cache, $allow_invalid) {
71 72 73
    if (!isset($cache->data)) {
      return FALSE;
    }
74 75 76 77 78 79 80
    // The object passed into this function is the one stored in $this->cache.
    // We must clone it as part of the preparation step so that the actual
    // cache object is not affected by the unserialize() call or other
    // manipulations of the returned object.

    $prepared = clone $cache;
    $prepared->data = unserialize($prepared->data);
81

82
    // Check expire time.
83
    $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
84

85
    if (!$allow_invalid && !$prepared->valid) {
86 87 88
      return FALSE;
    }

89
    return $prepared;
90 91 92
  }

  /**
93
   * {@inheritdoc}
94
   */
95
  public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
96
    assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
97 98 99
    $tags = array_unique($tags);
    // Sort the cache tags so that they are stored consistently in the database.
    sort($tags);
100 101
    $this->cache[$cid] = (object) array(
      'cid' => $cid,
102
      'data' => serialize($data),
103
      'created' => $this->getRequestTime(),
104
      'expire' => $expire,
105
      'tags' => $tags,
106 107 108
    );
  }

109 110 111 112 113 114 115 116 117
  /**
   * {@inheritdoc}
   */
  public function setMultiple(array $items = array()) {
    foreach ($items as $cid => $item) {
      $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : array());
    }
  }

118
  /**
119
   * {@inheritdoc}
120 121 122 123 124 125
   */
  public function delete($cid) {
    unset($this->cache[$cid]);
  }

  /**
126
   * {@inheritdoc}
127 128 129 130 131
   */
  public function deleteMultiple(array $cids) {
    $this->cache = array_diff_key($this->cache, array_flip($cids));
  }

132
  /**
133
   * {@inheritdoc}
134 135
   */
  public function deleteAll() {
136 137 138 139
    $this->cache = array();
  }

  /**
140
   * {@inheritdoc}
141 142
   */
  public function invalidate($cid) {
143
    if (isset($this->cache[$cid])) {
144
      $this->cache[$cid]->expire = $this->getRequestTime() - 1;
145
    }
146 147 148
  }

  /**
149
   * {@inheritdoc}
150
   */
151 152
  public function invalidateMultiple(array $cids) {
    foreach ($cids as $cid) {
153 154 155
      if (isset($this->cache[$cid])) {
        $this->cache[$cid]->expire = $this->getRequestTime() - 1;
      }
156 157 158 159
    }
  }

  /**
160
   * {@inheritdoc}
161 162 163
   */
  public function invalidateTags(array $tags) {
    foreach ($this->cache as $cid => $item) {
164
      if (array_intersect($tags, $item->tags)) {
165
        $this->cache[$cid]->expire = $this->getRequestTime() - 1;
166 167 168 169 170
      }
    }
  }

  /**
171
   * {@inheritdoc}
172 173 174
   */
  public function invalidateAll() {
    foreach ($this->cache as $cid => $item) {
175
      $this->cache[$cid]->expire = $this->getRequestTime() - 1;
176 177 178
    }
  }

179
  /**
180
   * {@inheritdoc}
181 182 183
   */
  public function garbageCollection() {
  }
184

185 186 187
  /**
   * {@inheritdoc}
   */
188 189 190
  public function removeBin() {
    $this->cache = [];
  }
191

192 193 194 195 196 197 198 199 200
  /**
   * Wrapper method for REQUEST_TIME constant.
   *
   * @return int
   */
  protected function getRequestTime() {
    return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
  }

201 202 203 204 205 206 207
  /**
   * Prevents data stored in memory backends from being serialized.
   */
  public function __sleep() {
    return [];
  }

208 209 210 211 212 213 214 215 216
  /**
   * Reset statically cached variables.
   *
   * This is only used by tests.
   */
  public function reset() {
    $this->cache = [];
  }

217
}