From 00d0fa7297919abf657593d21f6312f478a4e297 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 20 Jul 2016 13:53:17 +0100 Subject: [PATCH] Issue #2473179 by Fabianx, Berdir, dawehner: Use FileCache for config storage --- .../Core/Config/ExtensionInstallStorage.php | 3 +- core/lib/Drupal/Core/Config/FileStorage.php | 32 +++++++++++++++++-- .../lib/Drupal/Core/Config/InstallStorage.php | 3 +- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Config/ExtensionInstallStorage.php b/core/lib/Drupal/Core/Config/ExtensionInstallStorage.php index bdeb023ac57..14e80dd8869 100644 --- a/core/lib/Drupal/Core/Config/ExtensionInstallStorage.php +++ b/core/lib/Drupal/Core/Config/ExtensionInstallStorage.php @@ -44,9 +44,8 @@ class ExtensionInstallStorage extends InstallStorage { * search and to get overrides from. */ public function __construct(StorageInterface $config_storage, $directory = self::CONFIG_INSTALL_DIRECTORY, $collection = StorageInterface::DEFAULT_COLLECTION, $include_profile = TRUE) { + parent::__construct($directory, $collection); $this->configStorage = $config_storage; - $this->directory = $directory; - $this->collection = $collection; $this->includeProfile = $include_profile; } diff --git a/core/lib/Drupal/Core/Config/FileStorage.php b/core/lib/Drupal/Core/Config/FileStorage.php index eb485cfa154..25482033755 100644 --- a/core/lib/Drupal/Core/Config/FileStorage.php +++ b/core/lib/Drupal/Core/Config/FileStorage.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Config; +use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; @@ -24,6 +25,13 @@ class FileStorage implements StorageInterface { */ protected $directory = ''; + /** + * The file cache object. + * + * @var \Drupal\Component\FileCache\FileCacheInterface + */ + protected $fileCache; + /** * Constructs a new FileStorage. * @@ -36,6 +44,11 @@ class FileStorage implements StorageInterface { public function __construct($directory, $collection = StorageInterface::DEFAULT_COLLECTION) { $this->directory = $directory; $this->collection = $collection; + + // Use a NULL File Cache backend by default. This will ensure only the + // internal statc caching of FileCache is used and thus avoids blowing up + // the APCu cache. + $this->fileCache = FileCacheFactory::get('config', ['cache_backend_class' => NULL]); } /** @@ -90,7 +103,12 @@ class FileStorage implements StorageInterface { if (!$this->exists($name)) { return FALSE; } + $filepath = $this->getFilePath($name); + if ($data = $this->fileCache->get($filepath)) { + return $data; + } + $data = file_get_contents($filepath); try { $data = $this->decode($data); @@ -98,6 +116,8 @@ class FileStorage implements StorageInterface { catch (InvalidDataTypeException $e) { throw new UnsupportedDataTypeConfigException('Invalid data type in config ' . $name . ', found in file' . $filepath . ' : ' . $e->getMessage()); } + $this->fileCache->set($filepath, $data); + return $data; } @@ -119,18 +139,18 @@ class FileStorage implements StorageInterface { */ public function write($name, array $data) { try { - $data = $this->encode($data); + $encoded_data = $this->encode($data); } catch (InvalidDataTypeException $e) { throw new StorageException("Invalid data type in config $name: {$e->getMessage()}"); } $target = $this->getFilePath($name); - $status = @file_put_contents($target, $data); + $status = @file_put_contents($target, $encoded_data); if ($status === FALSE) { // Try to make sure the directory exists and try writing again. $this->ensureStorage(); - $status = @file_put_contents($target, $data); + $status = @file_put_contents($target, $encoded_data); } if ($status === FALSE) { throw new StorageException('Failed to write configuration file: ' . $this->getFilePath($name)); @@ -138,6 +158,9 @@ class FileStorage implements StorageInterface { else { drupal_chmod($target); } + + $this->fileCache->set($target, $data); + return TRUE; } @@ -152,6 +175,7 @@ class FileStorage implements StorageInterface { } return FALSE; } + $this->fileCache->delete($this->getFilePath($name)); return drupal_unlink($this->getFilePath($name)); } @@ -163,6 +187,8 @@ class FileStorage implements StorageInterface { if ($status === FALSE) { throw new StorageException('Failed to rename configuration file from: ' . $this->getFilePath($name) . ' to: ' . $this->getFilePath($new_name)); } + $this->fileCache->delete($this->getFilePath($name)); + $this->fileCache->delete($this->getFilePath($new_name)); return TRUE; } diff --git a/core/lib/Drupal/Core/Config/InstallStorage.php b/core/lib/Drupal/Core/Config/InstallStorage.php index a5e84943919..bd9bf3055f8 100644 --- a/core/lib/Drupal/Core/Config/InstallStorage.php +++ b/core/lib/Drupal/Core/Config/InstallStorage.php @@ -58,8 +58,7 @@ class InstallStorage extends FileStorage { * default collection. */ public function __construct($directory = self::CONFIG_INSTALL_DIRECTORY, $collection = StorageInterface::DEFAULT_COLLECTION) { - $this->directory = $directory; - $this->collection = $collection; + parent::__construct($directory, $collection); } /**