Commit 00d0fa72 authored by alexpott's avatar alexpott

Issue #2473179 by Fabianx, Berdir, dawehner: Use FileCache for config storage

parent b9fc15d6
......@@ -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;
}
......
......@@ -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 @@ public function read($name) {
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 @@ public function read($name) {
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 @@ public function readMultiple(array $names) {
*/
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 @@ public function write($name, array $data) {
else {
drupal_chmod($target);
}
$this->fileCache->set($target, $data);
return TRUE;
}
......@@ -152,6 +175,7 @@ public function delete($name) {
}
return FALSE;
}
$this->fileCache->delete($this->getFilePath($name));
return drupal_unlink($this->getFilePath($name));
}
......@@ -163,6 +187,8 @@ public function rename($name, $new_name) {
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;
}
......
......@@ -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);
}
/**
......
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