CachedStorage.php 8.12 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Config;

5
use Drupal\Core\Cache\CacheBackendInterface;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7 8

/**
9
 * Defines the cached storage.
10
 *
11
 * The class gets another storage and a cache backend injected. It reads from
12 13 14
 * the cache and delegates the read to the storage on a cache miss. It also
 * handles cache invalidation.
 */
15
class CachedStorage implements StorageInterface, StorageCacheInterface {
16
  use DependencySerializationTrait;
17 18 19 20

  /**
   * The configuration storage to be cached.
   *
21
   * @var \Drupal\Core\Config\StorageInterface
22 23 24 25 26 27
   */
  protected $storage;

  /**
   * The instantiated Cache backend.
   *
28
   * @var \Drupal\Core\Cache\CacheBackendInterface
29 30 31
   */
  protected $cache;

32 33 34 35 36 37 38
  /**
   * List of listAll() prefixes with their results.
   *
   * @var array
   */
  protected $findByPrefixCache = array();

39
  /**
40
   * Constructs a new CachedStorage.
41
   *
42
   * @param \Drupal\Core\Config\StorageInterface $storage
43
   *   A configuration storage to be cached.
44 45
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   A cache backend used to store configuration.
46
   */
47
  public function __construct(StorageInterface $storage, CacheBackendInterface $cache) {
48
    $this->storage = $storage;
49
    $this->cache = $cache;
50 51 52
  }

  /**
53
   * {@inheritdoc}
54 55 56 57 58 59 60 61 62
   */
  public function exists($name) {
    // The cache would read in the entire data (instead of only checking whether
    // any data exists), and on a potential cache miss, an additional storage
    // lookup would have to happen, so check the storage directly.
    return $this->storage->exists($name);
  }

  /**
63
   * {@inheritdoc}
64 65
   */
  public function read($name) {
66 67
    $cache_key = $this->getCacheKey($name);
    if ($cache = $this->cache->get($cache_key)) {
68 69 70
      // The cache contains either the cached configuration data or FALSE
      // if the configuration file does not exist.
      return $cache->data;
71
    }
72 73
    // Read from the storage on a cache miss and cache the data. Also cache
    // information about missing configuration objects.
74
    $data = $this->storage->read($name);
75
    $this->cache->set($cache_key, $data);
76 77 78
    return $data;
  }

79 80 81 82
  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) {
83 84 85 86 87 88 89 90 91 92 93 94 95
    $data_to_return = array();

    $cache_keys_map = $this->getCacheKeys($names);
    $cache_keys = array_values($cache_keys_map);
    $cached_list = $this->cache->getMultiple($cache_keys);

    if (!empty($cache_keys)) {
      // $cache_keys_map contains the full $name => $cache_key map, while
      // $cache_keys contains just the $cache_key values that weren't found in
      // the cache.
      // @see \Drupal\Core\Cache\CacheBackendInterface::getMultiple()
      $names_to_get = array_keys(array_intersect($cache_keys_map, $cache_keys));
      $list = $this->storage->readMultiple($names_to_get);
96 97
      // Cache configuration objects that were loaded from the storage, cache
      // missing configuration objects as an explicit FALSE.
98
      $items = array();
99 100 101 102
      foreach ($names_to_get as $name) {
        $data = isset($list[$name]) ? $list[$name] : FALSE;
        $data_to_return[$name] = $data;
        $items[$cache_keys_map[$name]] = array('data' => $data);
103
      }
104 105

      $this->cache->setMultiple($items);
106 107 108
    }

    // Add the configuration objects from the cache to the list.
109 110 111 112
    $cache_keys_inverse_map = array_flip($cache_keys_map);
    foreach ($cached_list as $cache_key => $cache) {
      $name = $cache_keys_inverse_map[$cache_key];
      $data_to_return[$name] = $cache->data;
113 114
    }

115 116
    // Ensure that only existing configuration objects are returned, filter out
    // cached information about missing objects.
117
    return array_filter($data_to_return);
118 119
  }

120
  /**
121
   * {@inheritdoc}
122 123 124 125 126
   */
  public function write($name, array $data) {
    if ($this->storage->write($name, $data)) {
      // While not all written data is read back, setting the cache instead of
      // just deleting it avoids cache rebuild stampedes.
127
      $this->cache->set($this->getCacheKey($name), $data);
128
      $this->findByPrefixCache = array();
129 130 131 132 133 134
      return TRUE;
    }
    return FALSE;
  }

  /**
135
   * {@inheritdoc}
136 137 138 139 140
   */
  public function delete($name) {
    // If the cache was the first to be deleted, another process might start
    // rebuilding the cache before the storage is gone.
    if ($this->storage->delete($name)) {
141
      $this->cache->delete($this->getCacheKey($name));
142
      $this->findByPrefixCache = array();
143 144 145 146 147 148
      return TRUE;
    }
    return FALSE;
  }

  /**
149
   * {@inheritdoc}
150 151 152 153 154
   */
  public function rename($name, $new_name) {
    // If the cache was the first to be deleted, another process might start
    // rebuilding the cache before the storage is renamed.
    if ($this->storage->rename($name, $new_name)) {
155 156
      $this->cache->delete($this->getCacheKey($name));
      $this->cache->delete($this->getCacheKey($new_name));
157
      $this->findByPrefixCache = array();
158 159 160 161 162 163
      return TRUE;
    }
    return FALSE;
  }

  /**
164
   * {@inheritdoc}
165 166 167 168 169 170
   */
  public function encode($data) {
    return $this->storage->encode($data);
  }

  /**
171
   * {@inheritdoc}
172 173 174 175 176 177
   */
  public function decode($raw) {
    return $this->storage->decode($raw);
  }

  /**
178
   * {@inheritdoc}
179 180
   */
  public function listAll($prefix = '') {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    // Do not cache when a prefix is not provided.
    if ($prefix) {
      return $this->findByPrefix($prefix);
    }
    return $this->storage->listAll();
  }

  /**
   * Finds configuration object names starting with a given prefix.
   *
   * Given the following configuration objects:
   * - node.type.article
   * - node.type.page
   *
   * Passing the prefix 'node.type.' will return an array containing the above
   * names.
   *
   * @param string $prefix
   *   The prefix to search for
   *
   * @return array
   *   An array containing matching configuration object names.
   */
  protected function findByPrefix($prefix) {
205 206
    $cache_key = $this->getCacheKey($prefix);
    if (!isset($this->findByPrefixCache[$cache_key])) {
207
      $this->findByPrefixCache[$cache_key] = $this->storage->listAll($prefix);
208
    }
209
    return $this->findByPrefixCache[$cache_key];
210
  }
211 212

  /**
213
   * {@inheritdoc}
214 215 216 217
   */
  public function deleteAll($prefix = '') {
    // If the cache was the first to be deleted, another process might start
    // rebuilding the cache before the storage is renamed.
218
    $names = $this->storage->listAll($prefix);
219
    if ($this->storage->deleteAll($prefix)) {
220
      $this->cache->deleteMultiple($this->getCacheKeys($names));
221 222 223 224
      return TRUE;
    }
    return FALSE;
  }
225 226 227 228 229 230 231

  /**
   * Clears the static list cache.
   */
  public function resetListCache() {
    $this->findByPrefixCache = array();
  }
232 233 234 235 236 237 238

  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) {
    return new static(
      $this->storage->createCollection($collection),
239
      $this->cache
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() {
    return $this->storage->getAllCollectionNames();
  }

  /**
   * {@inheritdoc}
   */
  public function getCollectionName() {
    return $this->storage->getCollectionName();
  }

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  /**
   * Returns a cache key for a configuration name using the collection.
   *
   * @param string $name
   *   The configuration name.
   *
   * @return string
   *   The cache key for the configuration name.
   */
  protected function getCacheKey($name) {
    return $this->getCollectionPrefix() . $name;
  }

  /**
   * Returns a cache key map for an array of configuration names.
   *
   * @param array $names
   *   The configuration names.
   *
   * @return array
   *   An array of cache keys keyed by configuration names.
   */
  protected function getCacheKeys(array $names) {
    $prefix = $this->getCollectionPrefix();
    $cache_keys = array_map(function($name) use ($prefix) {
      return $prefix . $name;
    }, $names);

    return array_combine($names, $cache_keys);
  }

  /**
   * Returns a cache ID prefix to use for the collection.
   *
   * @return string
   *   The cache ID prefix.
   */
  protected function getCollectionPrefix() {
    $collection = $this->storage->getCollectionName();
    if ($collection == StorageInterface::DEFAULT_COLLECTION) {
      return '';
    }
    return $collection . ':';
  }

302
}