Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add filecache config to enable/disable real purge #3263

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion caches/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

class FileCache implements CacheInterface
{
private array $config;
protected $path;
protected $key;

public function __construct()
public function __construct(array $config = [])
{
$this->config = $config;
if (!is_writable(PATH_CACHE)) {
throw new \Exception('The cache folder is not writeable');
}
Expand Down Expand Up @@ -46,6 +48,10 @@ public function getTime()

public function purgeCache($seconds)
{
if (! $this->config['enable_purge']) {
return;
}

$cachePath = $this->getPath();
if (!file_exists($cachePath)) {
return;
Expand Down
4 changes: 4 additions & 0 deletions config.default.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@

; --- Cache specific configuration ---------------------------------------------

[FileCache]
; Whether to actually delete files when purging. Can be useful to turn off to increase performance.
enable_purge = true

[SQLiteCache]
file = "cache.sqlite"

Expand Down
33 changes: 23 additions & 10 deletions lib/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@ public function create(string $name = null): CacheInterface
if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
$name = $matches[1];
}
if (in_array(strtolower($name), array_map('strtolower', $cacheNames))) {
$index = array_search(strtolower($name), array_map('strtolower', $cacheNames));
$name = $cacheNames[$index];
} else {

$index = array_search(strtolower($name), array_map('strtolower', $cacheNames));
if ($index === false) {
throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name));
}
if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name));
$className = $cacheNames[$index] . 'Cache';
if (!preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $className)) {
throw new \InvalidArgumentException(sprintf('Invalid cache classname: "%s"', $className));
}
$className = $name . 'Cache';
if (!file_exists(PATH_LIB_CACHES . $className . '.php')) {
throw new \Exception('Unable to find the cache file');

switch ($className) {
case NullCache::class:
return new NullCache();
case FileCache::class:
return new FileCache([
'enable_purge' => Configuration::getConfig('FileCache', 'enable_purge'),
]);
case SQLiteCache::class:
return new SQLiteCache();
case MemcachedCache::class:
return new MemcachedCache();
default:
if (!file_exists(PATH_LIB_CACHES . $className . '.php')) {
throw new \Exception('Unable to find the cache file');
}
return new $className();
}
return new $className();
}
}
2 changes: 1 addition & 1 deletion tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testFormatBytes()

public function testFileCache()
{
$sut = new \FileCache();
$sut = new \FileCache(['enable_purge' => true]);
$sut->setScope('scope');
$sut->purgeCache(-1);
$sut->setKey(['key']);
Expand Down