2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-10-08 14:52:03 +02:00
|
|
|
class FileCache implements CacheInterface
|
|
|
|
{
|
2023-09-21 22:05:55 +02:00
|
|
|
private Logger $logger;
|
2023-03-06 21:50:40 +01:00
|
|
|
private array $config;
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-21 22:05:55 +02:00
|
|
|
public function __construct(
|
|
|
|
Logger $logger,
|
|
|
|
array $config = []
|
|
|
|
) {
|
|
|
|
$this->logger = $logger;
|
2023-06-30 22:31:19 +02:00
|
|
|
$default = [
|
2023-07-06 15:59:38 +02:00
|
|
|
'path' => null,
|
|
|
|
'enable_purge' => true,
|
2023-06-30 22:31:19 +02:00
|
|
|
];
|
|
|
|
$this->config = array_merge($default, $config);
|
|
|
|
if (!$this->config['path']) {
|
|
|
|
throw new \Exception('The FileCache needs a path value');
|
2022-03-24 20:29:16 +00:00
|
|
|
}
|
2023-06-30 22:31:19 +02:00
|
|
|
// Normalize with a single trailing slash
|
|
|
|
$this->config['path'] = rtrim($this->config['path'], '/') . '/';
|
|
|
|
}
|
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function get(string $key, $default = null)
|
2016-09-10 20:41:11 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
$cacheFile = $this->createCacheFile($key);
|
|
|
|
if (!file_exists($cacheFile)) {
|
|
|
|
return $default;
|
2018-05-05 18:05:48 +01:00
|
|
|
}
|
2023-10-15 00:08:18 +02:00
|
|
|
$data = file_get_contents($cacheFile);
|
|
|
|
$item = unserialize($data);
|
2023-09-10 21:50:15 +02:00
|
|
|
if ($item === false) {
|
2023-09-21 22:05:55 +02:00
|
|
|
$this->logger->warning(sprintf('Failed to unserialize: %s', $cacheFile));
|
2023-09-10 21:50:15 +02:00
|
|
|
$this->delete($key);
|
|
|
|
return $default;
|
|
|
|
}
|
2023-09-24 20:53:07 +02:00
|
|
|
$expiration = $item['expiration'] ?? time();
|
2023-09-10 21:50:15 +02:00
|
|
|
if ($expiration === 0 || $expiration > time()) {
|
|
|
|
return $item['value'];
|
2023-06-30 22:31:19 +02:00
|
|
|
}
|
2023-09-10 21:50:15 +02:00
|
|
|
$this->delete($key);
|
|
|
|
return $default;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function set($key, $value, int $ttl = null): void
|
2019-04-29 20:12:43 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
$item = [
|
|
|
|
'key' => $key,
|
|
|
|
'expiration' => $ttl === null ? 0 : time() + $ttl,
|
2023-12-13 21:40:13 +01:00
|
|
|
'value' => $value,
|
2023-09-10 21:50:15 +02:00
|
|
|
];
|
|
|
|
$cacheFile = $this->createCacheFile($key);
|
2024-11-23 22:28:50 +01:00
|
|
|
$bytes = file_put_contents($cacheFile, serialize($item));
|
2024-11-28 03:50:56 +01:00
|
|
|
|
|
|
|
// TODO: Consider tightening the permissions of the created file.
|
|
|
|
// It usually allow others to read, depending on umask
|
|
|
|
|
2023-07-08 17:06:49 +02:00
|
|
|
if ($bytes === false) {
|
2024-11-28 03:50:56 +01:00
|
|
|
// Typically means no disk space remaining
|
|
|
|
$this->logger->warning(sprintf('Failed to write to: %s', $cacheFile));
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function delete(string $key): void
|
2016-09-10 20:41:11 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
unlink($this->createCacheFile($key));
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function clear(): void
|
2019-04-29 20:12:43 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
foreach (scandir($this->config['path']) as $filename) {
|
|
|
|
$cacheFile = $this->config['path'] . $filename;
|
|
|
|
$excluded = ['.' => true, '..' => true, '.gitkeep' => true];
|
|
|
|
if (isset($excluded[$filename]) || !is_file($cacheFile)) {
|
2022-08-06 22:46:28 +02:00
|
|
|
continue;
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2023-09-10 21:50:15 +02:00
|
|
|
unlink($cacheFile);
|
2016-10-07 22:33:45 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function prune(): void
|
2019-04-29 20:12:43 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
if (! $this->config['enable_purge']) {
|
|
|
|
return;
|
2019-04-29 20:12:43 +02:00
|
|
|
}
|
2023-09-10 21:50:15 +02:00
|
|
|
foreach (scandir($this->config['path']) as $filename) {
|
|
|
|
$cacheFile = $this->config['path'] . $filename;
|
|
|
|
$excluded = ['.' => true, '..' => true, '.gitkeep' => true];
|
|
|
|
if (isset($excluded[$filename]) || !is_file($cacheFile)) {
|
|
|
|
continue;
|
2019-04-29 20:12:43 +02:00
|
|
|
}
|
2023-10-15 00:08:18 +02:00
|
|
|
$data = file_get_contents($cacheFile);
|
|
|
|
$item = unserialize($data);
|
2023-09-10 21:50:15 +02:00
|
|
|
if ($item === false) {
|
|
|
|
unlink($cacheFile);
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-24 20:53:07 +02:00
|
|
|
$expiration = $item['expiration'] ?? time();
|
2023-09-10 21:50:15 +02:00
|
|
|
if ($expiration === 0 || $expiration > time()) {
|
2024-08-30 02:29:51 +02:00
|
|
|
// Cached forever, or not expired yet
|
2023-09-10 21:50:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-08-30 02:29:51 +02:00
|
|
|
// Expired, so delete file
|
2023-09-10 21:50:15 +02:00
|
|
|
unlink($cacheFile);
|
2015-11-05 10:12:58 +00:00
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
private function createCacheFile(string $key): string
|
2019-04-29 20:12:43 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
return $this->config['path'] . hash('md5', $key) . '.cache';
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2023-09-10 21:50:15 +02:00
|
|
|
public function getConfig()
|
2019-04-29 20:12:43 +02:00
|
|
|
{
|
2023-09-10 21:50:15 +02:00
|
|
|
return $this->config;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2015-11-05 10:12:58 +00:00
|
|
|
}
|