Commit 566bc8bd authored by alexpott's avatar alexpott

Issue #2442041 by Berdir: Remove CachedStorage::listAll() caching

parent ba500d47
......@@ -131,7 +131,6 @@ public function write($name, array $data) {
// While not all written data is read back, setting the cache instead of
// just deleting it avoids cache rebuild stampedes.
$this->cache->set($this->getCacheKey($name), $data);
Cache::invalidateTags(array($this::FIND_BY_PREFIX_CACHE_TAG));
$this->findByPrefixCache = array();
return TRUE;
}
......@@ -146,7 +145,6 @@ public function delete($name) {
// rebuilding the cache before the storage is gone.
if ($this->storage->delete($name)) {
$this->cache->delete($this->getCacheKey($name));
Cache::invalidateTags(array($this::FIND_BY_PREFIX_CACHE_TAG));
$this->findByPrefixCache = array();
return TRUE;
}
......@@ -162,7 +160,6 @@ public function rename($name, $new_name) {
if ($this->storage->rename($name, $new_name)) {
$this->cache->delete($this->getCacheKey($name));
$this->cache->delete($this->getCacheKey($new_name));
Cache::invalidateTags(array($this::FIND_BY_PREFIX_CACHE_TAG));
$this->findByPrefixCache = array();
return TRUE;
}
......@@ -213,20 +210,7 @@ public function listAll($prefix = '') {
protected function findByPrefix($prefix) {
$cache_key = $this->getCacheKey($prefix);
if (!isset($this->findByPrefixCache[$cache_key])) {
// The : character is not allowed in config file names, so this can not
// conflict.
if ($cache = $this->cache->get('find:' . $cache_key)) {
$this->findByPrefixCache[$cache_key] = $cache->data;
}
else {
$this->findByPrefixCache[$cache_key] = $this->storage->listAll($prefix);
$this->cache->set(
'find:' . $cache_key,
$this->findByPrefixCache[$cache_key],
Cache::PERMANENT,
array($this::FIND_BY_PREFIX_CACHE_TAG)
);
}
$this->findByPrefixCache[$cache_key] = $this->storage->listAll($prefix);
}
return $this->findByPrefixCache[$cache_key];
}
......
......@@ -12,14 +12,6 @@
*/
interface StorageCacheInterface {
/**
* Cache tag.
*
* Used by Drupal\Core\Config\CachedStorage::findByPrefix so that cached items
* can be cleared during writes, deletes and renames.
*/
const FIND_BY_PREFIX_CACHE_TAG = 'configFindByPrefix';
/**
* Reset the static cache of the listAll() cache.
*/
......
......@@ -4,7 +4,6 @@
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\CachedStorage;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Core\Cache\NullBackend;
/**
......@@ -39,133 +38,4 @@ public function testListAllStaticCache() {
$this->assertEquals($response, $cachedStorage->listAll($prefix));
}
/**
* Test CachedStorage::listAll() persistent cache.
*/
public function testListAllPrimedPersistentCache() {
$prefix = __FUNCTION__;
$storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$storage->expects($this->never())->method('listAll');
$response = array("$prefix." . $this->randomMachineName(), "$prefix." . $this->randomMachineName());
$cache = new MemoryBackend(__FUNCTION__);
$cache->set('find:' . $prefix, $response);
$cachedStorage = new CachedStorage($storage, $cache);
$this->assertEquals($response, $cachedStorage->listAll($prefix));
}
/**
* Test that we don't fall through to file storage with a primed cache.
*/
public function testGetMultipleOnPrimedCache() {
$configNames = array(
'foo.bar',
'baz.back',
);
$configCacheValues = array(
'foo.bar' => array(
'foo' => 'bar',
),
'baz.back' => array(
'foo' => 'bar',
),
);
$storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$storage->expects($this->never())->method('readMultiple');
$cache = new MemoryBackend(__FUNCTION__);
foreach ($configCacheValues as $key => $value) {
$cache->set($key, $value);
}
$cachedStorage = new CachedStorage($storage, $cache);
$this->assertEquals($configCacheValues, $cachedStorage->readMultiple($configNames));
}
/**
* Test fall through to file storage in CachedStorage::readMultiple().
*/
public function testGetMultipleOnPartiallyPrimedCache() {
$configNames = array(
'foo.bar',
'baz.back',
'config.exists_not_cached',
'config.does_not_exist_cached',
'config.does_not_exist',
);
$configCacheValues = array(
'foo.bar' => array(
'foo' => 'bar',
),
'baz.back' => array(
'foo' => 'bar',
),
);
$cache = new MemoryBackend(__FUNCTION__);
foreach ($configCacheValues as $key => $value) {
$cache->set($key, $value);
}
$cache->set('config.does_not_exist_cached', FALSE);
$config_exists_not_cached_data = array('foo' => 'bar');
$response = array(
$configNames[2] => $config_exists_not_cached_data,
$configNames[4] => FALSE,
);
$storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$storage->expects($this->once())
->method('readMultiple')
->with(array($configNames[2], $configNames[4]))
->will($this->returnValue($response));
$cachedStorage = new CachedStorage($storage, $cache);
$expected_data = $configCacheValues + array($configNames[2] => $config_exists_not_cached_data);
$this->assertEquals($expected_data, $cachedStorage->readMultiple($configNames));
// Ensure that the a missing file is cached.
$entry = $cache->get('config.does_not_exist');
$this->assertFalse($entry->data);
// Ensure that the a file containing data is cached.
$entry = $cache->get('config.exists_not_cached');
$this->assertEquals($config_exists_not_cached_data, $entry->data);
}
/**
* Test fall through to file storage on a cache miss in CachedStorage::read().
*/
public function testReadNonExistentFileCacheMiss() {
$name = 'config.does_not_exist';
$cache = new MemoryBackend(__FUNCTION__);
$storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$storage->expects($this->once())
->method('read')
->with($name)
->will($this->returnValue(FALSE));
$cachedStorage = new CachedStorage($storage, $cache);
$this->assertFalse($cachedStorage->read($name));
// Ensure that the a missing file is cached.
$entry = $cache->get('config.does_not_exist');
$this->assertFalse($entry->data);
}
/**
* Test file storage on a cache hit in CachedStorage::read().
*/
public function testReadNonExistentFileCached() {
$name = 'config.does_not_exist';
$cache = new MemoryBackend(__FUNCTION__);
$cache->set($name, FALSE);
$storage = $this->getMock('Drupal\Core\Config\StorageInterface');
$storage->expects($this->never())
->method('read');
$cachedStorage = new CachedStorage($storage, $cache);
$this->assertFalse($cachedStorage->read($name));
}
}
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