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-03-20 21:10:01 +03:00
|
|
|
protected $scope;
|
2019-04-29 21:12:43 +03:00
|
|
|
protected $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 = [
|
|
|
|
'path' => null,
|
|
|
|
'enable_purge' => true,
|
|
|
|
];
|
|
|
|
$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
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function saveData($data)
|
|
|
|
{
|
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($data));
|
2016-11-09 21:10:40 +03:00
|
|
|
if ($writeStream === false) {
|
2023-03-20 21:10:01 +03:00
|
|
|
throw new \Exception('The cache path is not writeable. You probably want: chown www-data:www-data cache');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
$cacheFile = $this->getCacheFile();
|
2018-08-25 22:00:51 +03:00
|
|
|
clearstatcache(false, $cacheFile);
|
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
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function purgeCache($seconds)
|
|
|
|
{
|
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) {
|
|
|
|
if (in_array($cacheFile->getBasename(), ['.', '..', '.gitkeep'])) {
|
|
|
|
continue;
|
|
|
|
} elseif ($cacheFile->isFile()) {
|
|
|
|
if (filemtime($cacheFile->getPathname()) < time() - $seconds) {
|
2022-10-26 01:47:45 +03:00
|
|
|
// todo: sometimes this file doesn't exists
|
2022-08-06 23:46:28 +03:00
|
|
|
unlink($cacheFile->getPathname());
|
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
|
|
|
}
|
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function setScope($scope)
|
|
|
|
{
|
2022-10-26 01:47:45 +03:00
|
|
|
if (!is_string($scope)) {
|
2019-04-29 21:12:43 +03:00
|
|
|
throw new \Exception('The given scope is invalid!');
|
2016-10-08 17:03:08 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-20 21:10:01 +03:00
|
|
|
$this->scope = $this->config['path'] . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-10-08 17:03:08 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function setKey($key)
|
|
|
|
{
|
|
|
|
$key = json_encode($key);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
if (!is_string($key)) {
|
|
|
|
throw new \Exception('The given key is invalid!');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
$this->key = $key;
|
2016-10-08 15:52:03 +03:00
|
|
|
return $this;
|
|
|
|
}
|
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
|
|
|
}
|