fix: file cache tweaks (#3470)

* fix: improve file cache

* fix(filecache): log when unserialize fails
This commit is contained in:
Dag 2023-06-30 22:31:19 +02:00 committed by GitHub
parent cc91ee1e37
commit 372880b5ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 13 deletions

View file

@ -8,22 +8,35 @@ class FileCache implements CacheInterface
public function __construct(array $config = [])
{
$this->config = $config;
$default = [
'path' => null,
'enable_purge' => true,
];
$this->config = array_merge($default, $config);
if (!$this->config['path']) {
throw new \Exception('The FileCache needs a path value');
}
// Normalize with a single trailing slash
$this->config['path'] = rtrim($this->config['path'], '/') . '/';
}
if (!is_dir($this->config['path'])) {
throw new \Exception('The cache path does not exists. You probably want: mkdir cache && chown www-data:www-data cache');
}
if (!is_writable($this->config['path'])) {
throw new \Exception('The cache path is not writeable. You probably want: chown www-data:www-data cache');
}
public function getConfig()
{
return $this->config;
}
public function loadData()
{
if (file_exists($this->getCacheFile())) {
return unserialize(file_get_contents($this->getCacheFile()));
if (!file_exists($this->getCacheFile())) {
return null;
}
return null;
$data = unserialize(file_get_contents($this->getCacheFile()));
if ($data === false) {
// Intentionally not throwing an exception
Logger::warning(sprintf('Failed to unserialize: %s', $this->getCacheFile()));
return null;
}
return $data;
}
public function saveData($data)