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,23 +8,36 @@ class FileCache implements CacheInterface
public function __construct(array $config = []) 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'])) { public function getConfig()
throw new \Exception('The cache path does not exists. You probably want: mkdir cache && chown www-data:www-data cache'); {
} return $this->config;
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 loadData() public function loadData()
{ {
if (file_exists($this->getCacheFile())) { if (!file_exists($this->getCacheFile())) {
return unserialize(file_get_contents($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) public function saveData($data)
{ {

View File

@ -38,11 +38,18 @@ class CacheFactory
case NullCache::class: case NullCache::class:
return new NullCache(); return new NullCache();
case FileCache::class: case FileCache::class:
return new FileCache([ $fileCacheConfig = [
// Intentionally checking for "truthy" value // Intentionally checking for truthy value because the historic default value is the empty string
'path' => Configuration::getConfig('FileCache', 'path') ?: PATH_CACHE, 'path' => Configuration::getConfig('FileCache', 'path') ?: PATH_CACHE,
'enable_purge' => Configuration::getConfig('FileCache', 'enable_purge'), 'enable_purge' => Configuration::getConfig('FileCache', 'enable_purge'),
]); ];
if (!is_dir($fileCacheConfig['path'])) {
throw new \Exception(sprintf('The FileCache path does not exists: %s', $fileCacheConfig['path']));
}
if (!is_writable($fileCacheConfig['path'])) {
throw new \Exception(sprintf('The FileCache path is not writable: %s', $fileCacheConfig['path']));
}
return new FileCache($fileCacheConfig);
case SQLiteCache::class: case SQLiteCache::class:
return new SQLiteCache(); return new SQLiteCache();
case MemcachedCache::class: case MemcachedCache::class:

View File

@ -6,6 +6,18 @@ use PHPUnit\Framework\TestCase;
class CacheTest extends TestCase class CacheTest extends TestCase
{ {
public function testConfig()
{
$sut = new \FileCache(['path' => '/tmp/']);
$this->assertSame(['path' => '/tmp/', 'enable_purge' => true], $sut->getConfig());
$sut = new \FileCache(['path' => '/', 'enable_purge' => false]);
$this->assertSame(['path' => '/', 'enable_purge' => false], $sut->getConfig());
$sut = new \FileCache(['path' => '/tmp', 'enable_purge' => true]);
$this->assertSame(['path' => '/tmp/', 'enable_purge' => true], $sut->getConfig());
}
public function testFileCache() public function testFileCache()
{ {
$temporaryFolder = sprintf('%s/rss_bridge_%s/', sys_get_temp_dir(), create_random_string()); $temporaryFolder = sprintf('%s/rss_bridge_%s/', sys_get_temp_dir(), create_random_string());