MemoryBackend.php 5.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
<?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.
 *
 */
class MemoryBackend implements CacheBackendInterface {

  /**
   * Array to store cache objects.
   */
  protected $cache;

  /**
   * All tags invalidated during the request.
   */
  protected $invalidatedTags = array();

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::__construct().
   */
  public function __construct($bin) {
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::get().
   */
  public function get($cid) {
    if (isset($this->cache[$cid])) {
      return $this->prepareItem($this->cache[$cid]);
    }
    else {
      return FALSE;
    }
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple().
   */
  public function getMultiple(&$cids) {
    $ret = array();

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

    foreach ($items as $item) {
      $item = $this->prepareItem($item);
      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.
   *
   * @param stdClass $cache
   *   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.
   */
  protected function prepareItem($cache) {
    if (!isset($cache->data)) {
      return FALSE;
    }

    // The cache data is invalid if any of its tags have been cleared since.
    if (count($cache->tags) && $this->hasInvalidatedTags($cache)) {
      return FALSE;
    }

    return $cache;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::set().
   */
  public function set($cid, $data, $expire = CACHE_PERMANENT, array $tags = array()) {
    $this->cache[$cid] = (object) array(
      'cid' => $cid,
      'data' => $data,
      'expire' => $expire,
      'tags' => $tags,
      'checksum' => $this->checksum($this->flattenTags($tags)),
    );
  }

  /*
   * Calculates a checksum so data can be invalidated using tags.
   */
  function checksum($tags) {
    $checksum = "";

    foreach($tags as $tag) {
      // Has the tag already been invalidated.
      if (isset($this->invalidatedTags[$tag])) {
        $checksum = $checksum . $tag . ':' . $this->invalidatedTags[$tag];
      }
    }

    return $checksum;
  }

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

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::deletePrefix().
   */
  public function deletePrefix($prefix) {
    foreach ($this->cache as $cid => $item) {
      if (strpos($cid, $prefix) === 0) {
        unset($this->cache[$cid]);
      }
    }
  }

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

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::expire().
   *
   * Cache expiration is not implemented for PHP ArrayBackend as this backend
   * only persists during a single request and expiration are done using
   * REQUEST_TIME.
   */
  public function expire() {
  }

  /**
   * Checks to see if any of the tags associated with a cache object have been
   * invalidated.
   *
   * @param object @cache
   *   An cache object to calculate and compare it's original checksum for.
   *
   * @return boolean
   *   TRUE if the a tag has been invalidated, FALSE otherwise.
   */
  protected function hasInvalidatedTags($cache) {
    if ($cache->checksum != $this->checksum($this->flattenTags($cache->tags))) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Flattens a tags array into a numeric array suitable for string storage.
   *
   * @param array $tags
   *   Associative array of tags to flatten.
   *
   * @return
   *   An array of flattened tag identifiers.
   */
  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 {
        $flat_tags["$namespace:$value"] = "$namespace:$values";
      }
    }
    return $flat_tags;
  }

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
   */
  public function invalidateTags(array $tags) {
    $flat_tags = $this->flattenTags($tags);
    foreach($flat_tags as $tag) {
      if (isset($this->invalidatedTags[$tag])) {
        $this->invalidatedTags[$tag] = $this->invalidatedTags[$tag] + 1;
      }
      else {
        $this->invalidatedTags[$tag] = 1;
      }
    }
  }

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

  /**
   * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection()
   */
  public function garbageCollection() {
  }
}