Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/State/State.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 4c8ae668cc8c |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Core\State; | 3 namespace Drupal\Core\State; |
4 | 4 |
5 use Drupal\Core\Cache\CacheBackendInterface; | |
6 use Drupal\Core\Cache\CacheCollector; | |
7 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; | 5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; |
8 use Drupal\Core\Lock\LockBackendInterface; | |
9 | 6 |
10 /** | 7 /** |
11 * Provides the state system using a key value store. | 8 * Provides the state system using a key value store. |
12 */ | 9 */ |
13 class State extends CacheCollector implements StateInterface { | 10 class State implements StateInterface { |
14 | 11 |
15 /** | 12 /** |
16 * The key value store to use. | 13 * The key value store to use. |
17 * | 14 * |
18 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface | 15 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface |
19 */ | 16 */ |
20 protected $keyValueStore; | 17 protected $keyValueStore; |
21 | 18 |
22 /** | 19 /** |
20 * Static state cache. | |
21 * | |
22 * @var array | |
23 */ | |
24 protected $cache = []; | |
25 | |
26 /** | |
23 * Constructs a State object. | 27 * Constructs a State object. |
24 * | 28 * |
25 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory | 29 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory |
26 * The key value store to use. | 30 * The key value store to use. |
27 * @param \Drupal\Core\Cache\CacheBackendInterface $cache | |
28 * The cache backend. | |
29 * @param \Drupal\Core\Lock\LockBackendInterface $lock | |
30 * The lock backend. | |
31 */ | 31 */ |
32 public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache, LockBackendInterface $lock) { | 32 public function __construct(KeyValueFactoryInterface $key_value_factory) { |
33 parent::__construct('state', $cache, $lock); | |
34 $this->keyValueStore = $key_value_factory->get('state'); | 33 $this->keyValueStore = $key_value_factory->get('state'); |
35 } | 34 } |
36 | 35 |
37 /** | 36 /** |
38 * {@inheritdoc} | 37 * {@inheritdoc} |
39 */ | 38 */ |
40 public function get($key, $default = NULL) { | 39 public function get($key, $default = NULL) { |
41 $value = parent::get($key); | 40 $values = $this->getMultiple([$key]); |
42 return $value !== NULL ? $value : $default; | 41 return isset($values[$key]) ? $values[$key] : $default; |
43 } | |
44 | |
45 /** | |
46 * {@inheritdoc} | |
47 */ | |
48 protected function resolveCacheMiss($key) { | |
49 $value = $this->keyValueStore->get($key); | |
50 $this->storage[$key] = $value; | |
51 $this->persist($key); | |
52 return $value; | |
53 } | 42 } |
54 | 43 |
55 /** | 44 /** |
56 * {@inheritdoc} | 45 * {@inheritdoc} |
57 */ | 46 */ |
58 public function getMultiple(array $keys) { | 47 public function getMultiple(array $keys) { |
59 $values = []; | 48 $values = []; |
49 $load = []; | |
60 foreach ($keys as $key) { | 50 foreach ($keys as $key) { |
61 $values[$key] = $this->get($key); | 51 // Check if we have a value in the cache. |
52 if (isset($this->cache[$key])) { | |
53 $values[$key] = $this->cache[$key]; | |
54 } | |
55 // Load the value if we don't have an explicit NULL value. | |
56 elseif (!array_key_exists($key, $this->cache)) { | |
57 $load[] = $key; | |
58 } | |
62 } | 59 } |
60 | |
61 if ($load) { | |
62 $loaded_values = $this->keyValueStore->getMultiple($load); | |
63 foreach ($load as $key) { | |
64 // If we find a value, even one that is NULL, add it to the cache and | |
65 // return it. | |
66 if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) { | |
67 $values[$key] = $loaded_values[$key]; | |
68 $this->cache[$key] = $loaded_values[$key]; | |
69 } | |
70 else { | |
71 $this->cache[$key] = NULL; | |
72 } | |
73 } | |
74 } | |
75 | |
63 return $values; | 76 return $values; |
64 } | 77 } |
65 | 78 |
66 /** | 79 /** |
67 * {@inheritdoc} | 80 * {@inheritdoc} |
68 */ | 81 */ |
69 public function set($key, $value) { | 82 public function set($key, $value) { |
70 parent::set($key, $value); | 83 $this->cache[$key] = $value; |
71 $this->keyValueStore->set($key, $value); | 84 $this->keyValueStore->set($key, $value); |
72 } | 85 } |
73 | 86 |
74 /** | 87 /** |
75 * {@inheritdoc} | 88 * {@inheritdoc} |
76 */ | 89 */ |
77 public function setMultiple(array $data) { | 90 public function setMultiple(array $data) { |
78 foreach ($data as $key => $value) { | 91 foreach ($data as $key => $value) { |
79 parent::set($key, $value); | 92 $this->cache[$key] = $value; |
80 } | 93 } |
81 $this->keyValueStore->setMultiple($data); | 94 $this->keyValueStore->setMultiple($data); |
82 } | 95 } |
83 | 96 |
84 /** | 97 /** |
85 * {@inheritdoc} | 98 * {@inheritdoc} |
86 */ | 99 */ |
87 public function delete($key) { | 100 public function delete($key) { |
88 parent::delete($key); | 101 $this->deleteMultiple([$key]); |
89 $this->keyValueStore->delete($key); | |
90 } | 102 } |
91 | 103 |
92 /** | 104 /** |
93 * {@inheritdoc} | 105 * {@inheritdoc} |
94 */ | 106 */ |
95 public function deleteMultiple(array $keys) { | 107 public function deleteMultiple(array $keys) { |
96 foreach ($keys as $key) { | 108 foreach ($keys as $key) { |
97 parent::delete($key); | 109 unset($this->cache[$key]); |
98 } | 110 } |
99 $this->keyValueStore->deleteMultiple($keys); | 111 $this->keyValueStore->deleteMultiple($keys); |
100 } | 112 } |
101 | 113 |
102 /** | 114 /** |
103 * {@inheritdoc} | 115 * {@inheritdoc} |
104 */ | 116 */ |
105 public function resetCache() { | 117 public function resetCache() { |
106 $this->clear(); | 118 $this->cache = []; |
107 } | 119 } |
108 | 120 |
109 } | 121 } |