MemoryBackend.php 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

/**
 * @file
 * Definition of Drupal\Core\Cache\ArrayBackend.
 */

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 21 22 23 24
 */
class MemoryBackend implements CacheBackendInterface {

  /**
   * 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 76 77 78 79 80
   *   An item loaded from cache_get() or cache_get_multiple().
   *
   * @return mixed
   *   The item with data as appropriate or FALSE if there is no
   *   valid item to load.
   */
81
  protected function prepareItem($cache, $allow_invalid) {
82 83 84 85
    if (!isset($cache->data)) {
      return FALSE;
    }

86
    // Check expire time.
87
    $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
88 89

    if (!$allow_invalid && !$cache->valid) {
90 91 92 93 94 95 96 97 98
      return FALSE;
    }

    return $cache;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::set().
   */
99
  public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
100 101 102
    $this->cache[$cid] = (object) array(
      'cid' => $cid,
      'data' => $data,
103
      'created' => REQUEST_TIME,
104
      'expire' => $expire,
105
      'tags' => $this->flattenTags($tags),
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    );
  }

  /**
   * 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));
  }

  /**
124
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags().
125
   */
126 127 128 129 130 131 132 133 134 135 136 137 138
  public function deleteTags(array $tags) {
    $flat_tags = $this->flattenTags($tags);
    foreach ($this->cache as $cid => $item) {
      if (array_intersect($flat_tags, $item->tags)) {
        unset($this->cache[$cid]);
      }
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll().
   */
  public function deleteAll() {
139 140 141 142
    $this->cache = array();
  }

  /**
143 144 145
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidate().
   */
  public function invalidate($cid) {
146 147 148
    if (isset($this->cache[$cid])) {
      $this->cache[$cid]->expire = REQUEST_TIME - 1;
    }
149 150 151 152
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
153
   */
154 155 156
  public function invalidateMultiple(array $cids) {
    foreach ($cids as $cid) {
      $this->cache[$cid]->expire = REQUEST_TIME - 1;
157 158 159 160
    }
  }

  /**
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
   */
  public function invalidateTags(array $tags) {
    $flat_tags = $this->flattenTags($tags);
    foreach ($this->cache as $cid => $item) {
      if (array_intersect($flat_tags, $item->tags)) {
        $this->cache[$cid]->expire = REQUEST_TIME - 1;
      }
    }
  }

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

  /**
   * 'Flattens' a tags array into an array of strings.
183 184 185 186
   *
   * @param array $tags
   *   Associative array of tags to flatten.
   *
187 188
   * @return array
   *   An indexed array of strings.
189 190 191 192 193 194 195 196 197 198 199 200 201 202
   */
  protected function flattenTags(array $tags) {
    if (isset($tags[0])) {
      return $tags;
    }

    $flat_tags = array();
    foreach ($tags as $namespace => $values) {
      if (is_array($values)) {
        foreach ($values as $value) {
          $flat_tags["$namespace:$value"] = "$namespace:$value";
        }
      }
      else {
203
        $flat_tags["$namespace:$values"] = "$namespace:$values";
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      }
    }
    return $flat_tags;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty().
   */
  public function isEmpty() {
    return empty($this->cache);
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection()
   */
  public function garbageCollection() {
  }
221 222 223 224 225 226

 /**
  * {@inheritdoc}
  */
  public function removeBin() {}

227
}