MemoryBackend.php 5.62 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Cache\MemoryBackend.
6 7 8 9 10 11 12 13 14 15 16 17
 */

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.
 *
18
 * @ingroup cache
19
 */
20
class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
21 22 23 24

  /**
   * Array to store cache objects.
   */
25
  protected $cache = array();
26 27

  /**
28 29 30 31
   * Constructs a MemoryBackend object.
   *
   * @param string $bin
   *   The cache bin for which the object is created.
32 33 34 35 36 37 38
   */
  public function __construct($bin) {
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::get().
   */
39
  public function get($cid, $allow_invalid = FALSE) {
40
    if (isset($this->cache[$cid])) {
41
      return $this->prepareItem($this->cache[$cid], $allow_invalid);
42 43 44 45 46 47 48 49 50
    }
    else {
      return FALSE;
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple().
   */
51
  public function getMultiple(&$cids, $allow_invalid = FALSE) {
52 53 54 55 56
    $ret = array();

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

    foreach ($items as $item) {
57
      $item = $this->prepareItem($item, $allow_invalid);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      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.
   *
74
   * @param object $cache
75
   *   An item loaded from cache_get() or cache_get_multiple().
76 77 78
   * @param bool $allow_invalid
   *   (optional) If TRUE, cache items may be returned even if they have expired
   *   or been invalidated.
79 80 81 82 83
   *
   * @return mixed
   *   The item with data as appropriate or FALSE if there is no
   *   valid item to load.
   */
84
  protected function prepareItem($cache, $allow_invalid) {
85 86 87
    if (!isset($cache->data)) {
      return FALSE;
    }
88 89 90 91 92 93 94
    // 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);
95

96
    // Check expire time.
97
    $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
98

99
    if (!$allow_invalid && !$prepared->valid) {
100 101 102
      return FALSE;
    }

103
    return $prepared;
104 105 106 107 108
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::set().
   */
109
  public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
110
    assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
111 112 113
    $tags = array_unique($tags);
    // Sort the cache tags so that they are stored consistently in the database.
    sort($tags);
114 115
    $this->cache[$cid] = (object) array(
      'cid' => $cid,
116
      'data' => serialize($data),
117
      'created' => $this->getRequestTime(),
118
      'expire' => $expire,
119
      'tags' => $tags,
120 121 122
    );
  }

123 124 125 126 127 128 129 130 131
  /**
   * {@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());
    }
  }

132 133 134 135 136 137 138 139 140 141 142 143 144 145
  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::delete().
   */
  public function delete($cid) {
    unset($this->cache[$cid]);
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
   */
  public function deleteMultiple(array $cids) {
    $this->cache = array_diff_key($this->cache, array_flip($cids));
  }

146 147 148 149
  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll().
   */
  public function deleteAll() {
150 151 152 153
    $this->cache = array();
  }

  /**
154 155 156
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidate().
   */
  public function invalidate($cid) {
157
    if (isset($this->cache[$cid])) {
158
      $this->cache[$cid]->expire = $this->getRequestTime() - 1;
159
    }
160 161 162 163
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
164
   */
165 166
  public function invalidateMultiple(array $cids) {
    foreach ($cids as $cid) {
167
      $this->cache[$cid]->expire = $this->getRequestTime() - 1;
168 169 170 171
    }
  }

  /**
172
   * {@inheritdoc}
173 174 175
   */
  public function invalidateTags(array $tags) {
    foreach ($this->cache as $cid => $item) {
176
      if (array_intersect($tags, $item->tags)) {
177
        $this->cache[$cid]->expire = $this->getRequestTime() - 1;
178 179 180 181 182 183 184 185 186
      }
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
   */
  public function invalidateAll() {
    foreach ($this->cache as $cid => $item) {
187
      $this->cache[$cid]->expire = $this->getRequestTime() - 1;
188 189 190
    }
  }

191 192 193 194 195
  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection()
   */
  public function garbageCollection() {
  }
196

197 198 199
  /**
   * {@inheritdoc}
   */
200 201 202
  public function removeBin() {
    $this->cache = [];
  }
203

204 205 206 207 208 209 210 211 212
  /**
   * Wrapper method for REQUEST_TIME constant.
   *
   * @return int
   */
  protected function getRequestTime() {
    return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
  }

213 214 215 216 217 218 219
  /**
   * Prevents data stored in memory backends from being serialized.
   */
  public function __sleep() {
    return [];
  }

220 221 222 223 224 225 226 227 228
  /**
   * Reset statically cached variables.
   *
   * This is only used by tests.
   */
  public function reset() {
    $this->cache = [];
  }

229
}