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