aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--caches/FileCache.php33
-rw-r--r--lib/CacheFactory.php13
-rw-r--r--tests/CacheTest.php12
3 files changed, 45 insertions, 13 deletions
diff --git a/caches/FileCache.php b/caches/FileCache.php
index 8c99234d..0e9d74d3 100644
--- a/caches/FileCache.php
+++ b/caches/FileCache.php
@@ -8,22 +8,35 @@ class FileCache implements CacheInterface
public function __construct(array $config = [])
{
- $this->config = $config;
-
- 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');
+ $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'], '/') . '/';
+ }
+
+ 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)
diff --git a/lib/CacheFactory.php b/lib/CacheFactory.php
index 4bf6342c..abafa3ba 100644
--- a/lib/CacheFactory.php
+++ b/lib/CacheFactory.php
@@ -38,11 +38,18 @@ class CacheFactory
case NullCache::class:
return new NullCache();
case FileCache::class:
- return new FileCache([
- // Intentionally checking for "truthy" value
+ $fileCacheConfig = [
+ // Intentionally checking for truthy value because the historic default value is the empty string
'path' => Configuration::getConfig('FileCache', 'path') ?: PATH_CACHE,
'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:
return new SQLiteCache();
case MemcachedCache::class:
diff --git a/tests/CacheTest.php b/tests/CacheTest.php
index 042fc7a1..f01cfc4b 100644
--- a/tests/CacheTest.php
+++ b/tests/CacheTest.php
@@ -6,6 +6,18 @@ use PHPUnit\Framework\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()
{
$temporaryFolder = sprintf('%s/rss_bridge_%s/', sys_get_temp_dir(), create_random_string());