Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / State / State.php
index cefcb6273e6bcae88681f8b8c84960bc18b07c2a..f35c954596fc260e31aff65816e553a418405089 100644 (file)
@@ -2,15 +2,12 @@
 
 namespace Drupal\Core\State;
 
-use Drupal\Core\Cache\CacheBackendInterface;
-use Drupal\Core\Cache\CacheCollector;
 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
-use Drupal\Core\Lock\LockBackendInterface;
 
 /**
  * Provides the state system using a key value store.
  */
-class State extends CacheCollector implements StateInterface {
+class State implements StateInterface {
 
   /**
    * The key value store to use.
@@ -19,18 +16,20 @@ class State extends CacheCollector implements StateInterface {
    */
   protected $keyValueStore;
 
+  /**
+   * Static state cache.
+   *
+   * @var array
+   */
+  protected $cache = [];
+
   /**
    * Constructs a State object.
    *
    * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
    *   The key value store to use.
-   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
-   *   The cache backend.
-   * @param \Drupal\Core\Lock\LockBackendInterface $lock
-   *   The lock backend.
    */
-  public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache, LockBackendInterface $lock) {
-    parent::__construct('state', $cache, $lock);
+  public function __construct(KeyValueFactoryInterface $key_value_factory) {
     $this->keyValueStore = $key_value_factory->get('state');
   }
 
@@ -38,18 +37,8 @@ class State extends CacheCollector implements StateInterface {
    * {@inheritdoc}
    */
   public function get($key, $default = NULL) {
-    $value = parent::get($key);
-    return $value !== NULL ? $value : $default;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  protected function resolveCacheMiss($key) {
-    $value = $this->keyValueStore->get($key);
-    $this->storage[$key] = $value;
-    $this->persist($key);
-    return $value;
+    $values = $this->getMultiple([$key]);
+    return isset($values[$key]) ? $values[$key] : $default;
   }
 
   /**
@@ -57,9 +46,33 @@ class State extends CacheCollector implements StateInterface {
    */
   public function getMultiple(array $keys) {
     $values = [];
+    $load = [];
     foreach ($keys as $key) {
-      $values[$key] = $this->get($key);
+      // Check if we have a value in the cache.
+      if (isset($this->cache[$key])) {
+        $values[$key] = $this->cache[$key];
+      }
+      // Load the value if we don't have an explicit NULL value.
+      elseif (!array_key_exists($key, $this->cache)) {
+        $load[] = $key;
+      }
     }
+
+    if ($load) {
+      $loaded_values = $this->keyValueStore->getMultiple($load);
+      foreach ($load as $key) {
+        // If we find a value, even one that is NULL, add it to the cache and
+        // return it.
+        if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) {
+          $values[$key] = $loaded_values[$key];
+          $this->cache[$key] = $loaded_values[$key];
+        }
+        else {
+          $this->cache[$key] = NULL;
+        }
+      }
+    }
+
     return $values;
   }
 
@@ -67,7 +80,7 @@ class State extends CacheCollector implements StateInterface {
    * {@inheritdoc}
    */
   public function set($key, $value) {
-    parent::set($key, $value);
+    $this->cache[$key] = $value;
     $this->keyValueStore->set($key, $value);
   }
 
@@ -76,7 +89,7 @@ class State extends CacheCollector implements StateInterface {
    */
   public function setMultiple(array $data) {
     foreach ($data as $key => $value) {
-      parent::set($key, $value);
+      $this->cache[$key] = $value;
     }
     $this->keyValueStore->setMultiple($data);
   }
@@ -85,8 +98,7 @@ class State extends CacheCollector implements StateInterface {
    * {@inheritdoc}
    */
   public function delete($key) {
-    parent::delete($key);
-    $this->keyValueStore->delete($key);
+    $this->deleteMultiple([$key]);
   }
 
   /**
@@ -94,7 +106,7 @@ class State extends CacheCollector implements StateInterface {
    */
   public function deleteMultiple(array $keys) {
     foreach ($keys as $key) {
-      parent::delete($key);
+      unset($this->cache[$key]);
     }
     $this->keyValueStore->deleteMultiple($keys);
   }
@@ -103,7 +115,7 @@ class State extends CacheCollector implements StateInterface {
    * {@inheritdoc}
    */
   public function resetCache() {
-    $this->clear();
+    $this->cache = [];
   }
 
 }