diff --git a/core/lib/Drupal/Core/Config/CachedStorage.php b/core/lib/Drupal/Core/Config/CachedStorage.php index 1ff4046..cfb06ce 100644 --- a/core/lib/Drupal/Core/Config/CachedStorage.php +++ b/core/lib/Drupal/Core/Config/CachedStorage.php @@ -41,6 +41,13 @@ class CachedStorage implements StorageInterface, StorageCacheInterface { protected $findByPrefixCache = array(); /** + * The cache prefix to use for all cache entries. + * + * @var string + */ + protected $cachePrefix = NULL; + + /** * Constructs a new CachedStorage controller. * * @param \Drupal\Core\Config\StorageInterface $storage @@ -67,7 +74,7 @@ public function exists($name) { * Implements Drupal\Core\Config\StorageInterface::read(). */ public function read($name) { - if ($cache = $this->cache->get($name)) { + if ($cache = $this->cache->get($this->getCacheKey($name))) { // The cache contains either the cached configuration data or FALSE // if the configuration file does not exist. return $cache->data; @@ -75,7 +82,7 @@ public function read($name) { // Read from the storage on a cache miss and cache the data. Also cache // information about missing configuration objects. $data = $this->storage->read($name); - $this->cache->set($name, $data); + $this->cache->set($this->getCacheKey($name), $data); return $data; } @@ -87,14 +94,15 @@ public function readMultiple(array $names) { // The names array is passed by reference and will only contain the names of // config object not found after the method call. // @see \Drupal\Core\Cache\CacheBackendInterface::getMultiple() - $cached_list = $this->cache->getMultiple($names); + $cids = $this->getCacheKeys($names); + $cached_list = $this->cache->getMultiple($cids); if (!empty($names)) { $list = $this->storage->readMultiple($names); // Cache configuration objects that were loaded from the storage, cache // missing configuration objects as an explicit FALSE. foreach ($names as $name) { - $this->cache->set($name, isset($list[$name]) ? $list[$name] : FALSE); + $this->cache->set($this->getCacheKey($name), isset($list[$name]) ? $list[$name] : FALSE); } } @@ -115,7 +123,8 @@ 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. - $this->cache->set($name, $data); + $this->cache->set($this->getCacheKey($name), $data); + // @todo: How to deal with the prefix for the tag? Cache::deleteTags(array($this::FIND_BY_PREFIX_CACHE_TAG => TRUE)); $this->findByPrefixCache = array(); return TRUE; @@ -130,7 +139,7 @@ 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)) { - $this->cache->delete($name); + $this->cache->delete($this->getCacheKey($name)); Cache::deleteTags(array($this::FIND_BY_PREFIX_CACHE_TAG => TRUE)); $this->findByPrefixCache = array(); return TRUE; @@ -145,8 +154,8 @@ 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)) { - $this->cache->delete($name); - $this->cache->delete($new_name); + $this->cache->delete($this->getCacheKey($name)); + $this->cache->delete($this->getCacheKey($new_name)); Cache::deleteTags(array($this::FIND_BY_PREFIX_CACHE_TAG => TRUE)); $this->findByPrefixCache = array(); return TRUE; @@ -199,13 +208,13 @@ protected function findByPrefix($prefix) { if (!isset($this->findByPrefixCache[$prefix])) { // The : character is not allowed in config file names, so this can not // conflict. - if ($cache = $this->cache->get('find:' . $prefix)) { + if ($cache = $this->cache->get($this->getCacheKey('find:' . $prefix))) { $this->findByPrefixCache[$prefix] = $cache->data; } else { $this->findByPrefixCache[$prefix] = $this->storage->listAll($prefix); $this->cache->set( - 'find:' . $prefix, + $this->getCacheKey('find:' . $prefix), $this->findByPrefixCache[$prefix], Cache::PERMANENT, array($this::FIND_BY_PREFIX_CACHE_TAG => TRUE) @@ -221,9 +230,9 @@ protected function findByPrefix($prefix) { 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. - $cids = $this->storage->listAll($prefix); + $names = $this->storage->listAll($prefix); if ($this->storage->deleteAll($prefix)) { - $this->cache->deleteMultiple($cids); + $this->cache->deleteMultiple($this->getCacheKeys($names)); return TRUE; } return FALSE; @@ -235,4 +244,35 @@ public function deleteAll($prefix = '') { public function resetListCache() { $this->findByPrefixCache = array(); } + + /** + * Builds the cache key for a config name. + * + * @param string $name + * Name of the config file. + * + * @return string + * The cache key. + */ + protected function getCacheKey($name) { + return $this->cachePrefix ? $this->cachePrefix . ':' . $name : $name; + } + + /** + * Builds the cache key for a list of config names. + * + * @param array $names + * List of config file names. + * + * @return array + * List of cache keys, keyed by config name. + */ + protected function getCacheKeys(array $names) { + $cids = array(); + foreach ($names as $name) { + $cids[$name] = $this->getCacheKey($name); + } + return $cids; + } + } diff --git a/core/modules/language/language.services.yml b/core/modules/language/language.services.yml index 83a31ff..bfeee31 100644 --- a/core/modules/language/language.services.yml +++ b/core/modules/language/language.services.yml @@ -15,15 +15,14 @@ services: class: Drupal\Core\Cache\CacheBackendInterface tags: - { name: cache.bin } - - { name: persist } factory_method: get factory_service: cache_factory arguments: [language_config] + language.config_storage.file: + class: Drupal\language\Config\LanguageOverrideFileStorage language.config_storage: - class: Drupal\language\Config\LanguageOverrideStorage - arguments: ['@language.cache_config'] - tags: - - { name: persist } + class: Drupal\language\Config\LanguageOverrideCachedStorage + arguments: ['@language.config_storage.file', '@language.cache_config'] language.config_factory_override: class: Drupal\language\Config\LanguageConfigFactoryOverride arguments: ['@language.config_storage', '@event_dispatcher', '@config.typed'] diff --git a/core/modules/language/lib/Drupal/language/Config/LanguageConfigFactoryOverride.php b/core/modules/language/lib/Drupal/language/Config/LanguageConfigFactoryOverride.php index 4c678ff..340352f 100644 --- a/core/modules/language/lib/Drupal/language/Config/LanguageConfigFactoryOverride.php +++ b/core/modules/language/lib/Drupal/language/Config/LanguageConfigFactoryOverride.php @@ -71,6 +71,7 @@ public function loadOverrides($names) { if ($this->language) { return $this->storage->readMultiple($names); } + return array(); } /** diff --git a/core/modules/language/lib/Drupal/language/Config/LanguageOverrideCachedStorage.php b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideCachedStorage.php new file mode 100644 index 0000000..40983d6 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideCachedStorage.php @@ -0,0 +1,46 @@ +cachePrefix = $langcode; + $this->storage->setLangcode($langcode); + return $this; + } + +} diff --git a/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorage.php b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideFileStorage.php similarity index 65% rename from core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorage.php rename to core/modules/language/lib/Drupal/language/Config/LanguageOverrideFileStorage.php index e9a10cb..fa31003 100644 --- a/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorage.php +++ b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideFileStorage.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\language\Config\LanguageOverrideStorage. + * Contains \Drupal\language\Config\LanguageOverrideFileStorage. */ namespace Drupal\language\Config; @@ -12,18 +12,25 @@ use Drupal\Core\Config\FileStorage; /** - * Defines storage for language configuration overrides. + * Defines file storage for language configuration overrides. */ -class LanguageOverrideStorage extends CachedStorage implements LanguageOverrideStorageInterface { +class LanguageOverrideFileStorage extends FileStorage implements LanguageOverrideStorageInterface { /** - * Language code that determines the configuration override storage directory. + * The directory for the current language. * * @var string */ protected $langcode; /** + * The directory for the current language. + * + * @var string + */ + protected $directory; + + /** * Tracks whether the directory exists to prevent excessive file system reads. * * @var bool @@ -31,13 +38,10 @@ class LanguageOverrideStorage extends CachedStorage implements LanguageOverrideS protected $directoryExists; /** - * Constructs a new LanguageOverrideStorage. - * - * @param \Drupal\Core\Cache\CacheBackendInterface $cache - * A cache backend instance to use for caching. + * Constructs a new LanguageOverrideFileStorage. */ - public function __construct(CacheBackendInterface $cache) { - $this->cache = $cache; + public function __construct() { + // Do not call the parent as we do not yet have a directory. } /** @@ -109,41 +113,6 @@ public function setLangcode($langcode) { } /** - * {@inheritdoc} - */ - protected function getCacheKey($name) { - return $this->langcode . ':' . $name; - } - - /** - * {@inheritdoc} - */ - protected function getCacheKeys(array $names) { - foreach ($names as $name) { - $names[$name] = $this->getCacheKey($name); - } - return $names; - } - - /** - * {@inheritdoc} - */ - protected function getNameFromCacheKey($key) { - // Remove everything before the semicolon. - return substr($key, strpos($key, ':') + 1); - } - - /** - * {@inheritdoc} - */ - protected function getNamesFromCacheKeys($keys) { - foreach ($keys as $key) { - $keys[$key] = $this->getNameFromCacheKey($key); - } - return $keys; - } - - /** * Discovers is the directory for the language exists. * * @return bool diff --git a/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorageInterface.php b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorageInterface.php index 5e31cc8..ba62497 100644 --- a/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorageInterface.php +++ b/core/modules/language/lib/Drupal/language/Config/LanguageOverrideStorageInterface.php @@ -10,7 +10,7 @@ use Drupal\Core\Config\StorageCacheInterface; use Drupal\Core\Config\StorageInterface; -interface LanguageOverrideStorageInterface extends StorageInterface, StorageCacheInterface { +interface LanguageOverrideStorageInterface extends StorageInterface { /** * Sets the langcode to determine the override configuration directory to use. diff --git a/core/modules/locale/tests/modules/locale_test/config/language.config.de.locale_test.translation.yml b/core/modules/locale/tests/modules/locale_test/config/language/de/locale_test.translation.yml similarity index 100% rename from core/modules/locale/tests/modules/locale_test/config/language.config.de.locale_test.translation.yml rename to core/modules/locale/tests/modules/locale_test/config/language/de/locale_test.translation.yml