2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-10-08 15:52:03 +03:00
|
|
|
class FileCache implements CacheInterface
|
|
|
|
{
|
2023-03-06 23:50:40 +03:00
|
|
|
private array $config;
|
2023-07-06 16:10:30 +03:00
|
|
|
protected string $scope;
|
|
|
|
protected string $key;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-06 23:50:40 +03:00
|
|
|
public function __construct(array $config = [])
|
2022-03-24 23:29:16 +03:00
|
|
|
{
|
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'], '/') . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function loadData()
|
|
|
|
{
|
2023-06-30 23:31:19 +03:00
|
|
|
if (!file_exists($this->getCacheFile())) {
|
|
|
|
return null;
|
2018-05-05 20:05:48 +03:00
|
|
|
}
|
2023-06-30 23:31:19 +03:00
|
|
|
$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;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function saveData($data): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-08 18:06:49 +03:00
|
|
|
$bytes = file_put_contents($this->getCacheFile(), serialize($data), LOCK_EX);
|
|
|
|
if ($bytes === false) {
|
|
|
|
throw new \Exception(sprintf('Failed to write to: %s', $this->getCacheFile()));
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-05 18:37:21 +03:00
|
|
|
public function getTime(): ?int
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
2023-07-06 20:39:40 +03:00
|
|
|
// https://www.php.net/manual/en/function.clearstatcache.php
|
|
|
|
clearstatcache();
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$cacheFile = $this->getCacheFile();
|
2017-07-29 20:28:00 +03:00
|
|
|
if (file_exists($cacheFile)) {
|
2019-04-29 21:12:43 +03:00
|
|
|
$time = filemtime($cacheFile);
|
2022-08-06 23:46:28 +03:00
|
|
|
if ($time !== false) {
|
|
|
|
return $time;
|
|
|
|
}
|
|
|
|
return null;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
return null;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function purgeCache(int $seconds): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-03-06 23:50:40 +03:00
|
|
|
if (! $this->config['enable_purge']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-20 21:10:01 +03:00
|
|
|
$cachePath = $this->getScope();
|
2022-08-06 23:46:28 +03:00
|
|
|
if (!file_exists($cachePath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$cacheIterator = new \RecursiveIteratorIterator(
|
|
|
|
new \RecursiveDirectoryIterator($cachePath),
|
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($cacheIterator as $cacheFile) {
|
2023-07-06 19:52:19 +03:00
|
|
|
$basename = $cacheFile->getBasename();
|
|
|
|
$excluded = [
|
|
|
|
'.' => true,
|
|
|
|
'..' => true,
|
|
|
|
'.gitkeep' => true,
|
|
|
|
];
|
|
|
|
if (isset($excluded[$basename])) {
|
2022-08-06 23:46:28 +03:00
|
|
|
continue;
|
|
|
|
} elseif ($cacheFile->isFile()) {
|
2023-07-06 19:52:19 +03:00
|
|
|
$filepath = $cacheFile->getPathname();
|
|
|
|
if (filemtime($filepath) < time() - $seconds) {
|
2022-10-26 01:47:45 +03:00
|
|
|
// todo: sometimes this file doesn't exists
|
2023-07-06 19:52:19 +03:00
|
|
|
unlink($filepath);
|
2016-10-07 23:33:45 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2016-10-07 23:33:45 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function setScope(string $scope): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-03-20 21:10:01 +03:00
|
|
|
$this->scope = $this->config['path'] . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
|
2016-10-08 17:03:08 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-06 16:10:30 +03:00
|
|
|
public function setKey(array $key): void
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-07-06 16:10:30 +03:00
|
|
|
$this->key = json_encode($key);
|
2016-10-08 15:52:03 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-20 21:10:01 +03:00
|
|
|
private function getScope()
|
2019-04-29 21:12:43 +03:00
|
|
|
{
|
2023-03-20 21:10:01 +03:00
|
|
|
if (is_null($this->scope)) {
|
2019-04-29 21:12:43 +03:00
|
|
|
throw new \Exception('Call "setScope" first!');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-20 21:10:01 +03:00
|
|
|
if (!is_dir($this->scope)) {
|
|
|
|
if (mkdir($this->scope, 0755, true) !== true) {
|
2022-08-06 23:46:28 +03:00
|
|
|
throw new \Exception('mkdir: Unable to create file cache folder');
|
2019-04-29 21:12:43 +03:00
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-20 21:10:01 +03:00
|
|
|
return $this->scope;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
private function getCacheFile()
|
|
|
|
{
|
2023-03-20 21:10:01 +03:00
|
|
|
return $this->getScope() . $this->getCacheName();
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
private function getCacheName()
|
|
|
|
{
|
|
|
|
if (is_null($this->key)) {
|
|
|
|
throw new \Exception('Call "setKey" first!');
|
2016-10-08 16:21:10 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
return hash('md5', $this->key) . '.cache';
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|