Commit 7fcabc7a authored by catch's avatar catch

Issue #2575105 by Berdir, catch, amateescu: Use cache collector for state (revert)

parent 4dfab43f
......@@ -439,9 +439,7 @@ services:
factory: Drupal\Core\Site\Settings::getInstance
state:
class: Drupal\Core\State\State
arguments: ['@keyvalue', '@cache.bootstrap', '@lock']
tags:
- { name: needs_destruction }
arguments: ['@keyvalue']
queue:
class: Drupal\Core\Queue\QueueFactory
arguments: ['@settings']
......
......@@ -157,9 +157,7 @@ public function invalidate($cid) {
*/
public function invalidateMultiple(array $cids) {
foreach ($cids as $cid) {
if (isset($this->cache[$cid])) {
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
}
......
......@@ -60,10 +60,7 @@ public function onKernelTerminate(PostResponseEvent $event) {
* An array of event listener definitions.
*/
public static function getSubscribedEvents() {
// Run this subscriber after others as those might use services that need
// to be terminated as well or run code that needs to run before
// termination.
$events[KernelEvents::TERMINATE][] = ['onKernelTerminate', -100];
$events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 100];
return $events;
}
......
......@@ -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 @@ public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBa
* {@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 @@ protected function resolveCacheMiss($key) {
*/
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 @@ public function getMultiple(array $keys) {
* {@inheritdoc}
*/
public function set($key, $value) {
parent::set($key, $value);
$this->cache[$key] = $value;
$this->keyValueStore->set($key, $value);
}
......@@ -76,7 +89,7 @@ public function set($key, $value) {
*/
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 @@ public function setMultiple(array $data) {
* {@inheritdoc}
*/
public function delete($key) {
parent::delete($key);
$this->keyValueStore->delete($key);
$this->deleteMultiple([$key]);
}
/**
......@@ -94,7 +106,7 @@ public function delete($key) {
*/
public function deleteMultiple(array $keys) {
foreach ($keys as $key) {
parent::delete($key);
unset($this->cache[$key]);
}
$this->keyValueStore->deleteMultiple($keys);
}
......@@ -103,7 +115,7 @@ public function deleteMultiple(array $keys) {
* {@inheritdoc}
*/
public function resetCache() {
$this->clear();
$this->cache = [];
}
}
......@@ -2,9 +2,7 @@
namespace Drupal\KernelTests\Core\Routing;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Lock\NullLockBackend;
use Drupal\Core\State\State;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Routing\Route;
......@@ -38,7 +36,7 @@ protected function setUp() {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
$this->state = new State(new KeyValueMemoryFactory());
}
/**
......
......@@ -12,7 +12,6 @@
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Lock\NullLockBackend;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Routing\MatcherDumper;
use Drupal\Core\Routing\RouteProvider;
......@@ -84,7 +83,7 @@ class RouteProviderTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
$this->state = new State(new KeyValueMemoryFactory());
$this->currentPath = new CurrentPathStack(new RequestStack());
$this->cache = new MemoryBackend();
$this->pathProcessor = \Drupal::service('path_processor_manager');
......
......@@ -7,12 +7,10 @@
namespace Drupal\Tests\Core\Extension;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\InfoParser;
use Drupal\Core\Extension\ThemeHandler;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Lock\NullLockBackend;
use Drupal\Core\State\State;
use Drupal\Tests\UnitTestCase;
......@@ -80,7 +78,7 @@ protected function setUp() {
],
]);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
$this->state = new State(new KeyValueMemoryFactory());
$this->infoParser = $this->getMock('Drupal\Core\Extension\InfoParserInterface');
$this->extensionDiscovery = $this->getMockBuilder('Drupal\Core\Extension\ExtensionDiscovery')
->disableOriginalConstructor()
......
......@@ -9,7 +9,6 @@
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\Lock\NullLockBackend;
use Drupal\Core\State\State;
use Drupal\Core\Cache\Cache;
......@@ -539,7 +538,7 @@ public function testBubblingWithPrerender($test_element) {
$this->setupMemoryCache();
// Mock the State service.
$memory_state = new State(new KeyValueMemoryFactory(), new MemoryBackend('test'), new NullLockBackend());
$memory_state = new State(new KeyValueMemoryFactory());
\Drupal::getContainer()->set('state', $memory_state);
$this->controllerResolver->expects($this->any())
->method('getControllerFromDefinition')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment