Issue #2473179 by Fabianx, Berdir, dawehner: Use FileCache for config storage
parent
b9fc15d622
commit
00d0fa7297
|
@ -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 @@ 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue