2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2016-10-08 14:52:03 +02:00
|
|
|
class FileCache implements CacheInterface
|
|
|
|
{
|
2016-10-08 16:03:08 +02:00
|
|
|
protected $path;
|
2019-04-29 20:12:43 +02:00
|
|
|
protected $key;
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2022-03-24 20:29:16 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!is_writable(PATH_CACHE)) {
|
2022-08-06 22:46:28 +02:00
|
|
|
throw new \Exception('The cache folder is not writeable');
|
2022-03-24 20:29:16 +00:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function loadData()
|
|
|
|
{
|
2018-05-05 18:05:48 +01:00
|
|
|
if (file_exists($this->getCacheFile())) {
|
|
|
|
return unserialize(file_get_contents($this->getCacheFile()));
|
|
|
|
}
|
2019-04-29 20:12:43 +02:00
|
|
|
return null;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function saveData($data)
|
|
|
|
{
|
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($data));
|
2016-11-09 19:10:40 +01:00
|
|
|
if ($writeStream === false) {
|
2018-06-29 23:55:33 +02:00
|
|
|
throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function getTime()
|
|
|
|
{
|
|
|
|
$cacheFile = $this->getCacheFile();
|
2018-08-26 00:00:51 +05:00
|
|
|
clearstatcache(false, $cacheFile);
|
2017-07-29 19:28:00 +02:00
|
|
|
if (file_exists($cacheFile)) {
|
2019-04-29 20:12:43 +02:00
|
|
|
$time = filemtime($cacheFile);
|
2022-08-06 22:46:28 +02:00
|
|
|
if ($time !== false) {
|
|
|
|
return $time;
|
|
|
|
}
|
|
|
|
return null;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
return null;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function purgeCache($seconds)
|
|
|
|
{
|
2016-10-08 16:03:08 +02:00
|
|
|
$cachePath = $this->getPath();
|
2022-08-06 22:46:28 +02: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 00:47:45 +02:00
|
|
|
// todo: sometimes this file doesn't exists
|
2022-08-06 22:46:28 +02:00
|
|
|
unlink($cacheFile->getPathname());
|
2016-10-07 22:33:45 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
2016-10-07 22:33:45 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function setScope($scope)
|
|
|
|
{
|
2022-10-26 00:47:45 +02:00
|
|
|
if (!is_string($scope)) {
|
2019-04-29 20:12:43 +02:00
|
|
|
throw new \Exception('The given scope is invalid!');
|
2016-10-08 16:03:08 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
$this->path = PATH_CACHE . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2016-10-08 16:03:08 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
public function setKey($key)
|
|
|
|
{
|
|
|
|
$key = json_encode($key);
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
if (!is_string($key)) {
|
|
|
|
throw new \Exception('The given key is invalid!');
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
$this->key = $key;
|
2016-10-08 14:52:03 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
private function getPath()
|
|
|
|
{
|
2017-07-29 19:28:00 +02:00
|
|
|
if (is_null($this->path)) {
|
2019-04-29 20:12:43 +02:00
|
|
|
throw new \Exception('Call "setScope" first!');
|
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
if (!is_dir($this->path)) {
|
|
|
|
if (mkdir($this->path, 0755, true) !== true) {
|
2022-08-06 22:46:28 +02:00
|
|
|
throw new \Exception('mkdir: Unable to create file cache folder');
|
2019-04-29 20:12:43 +02:00
|
|
|
}
|
2015-11-05 10:12:58 +00:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2016-10-08 16:03:08 +02:00
|
|
|
return $this->path;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
private function getCacheFile()
|
|
|
|
{
|
2016-10-08 16:03:08 +02:00
|
|
|
return $this->getPath() . $this->getCacheName();
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
private function getCacheName()
|
|
|
|
{
|
|
|
|
if (is_null($this->key)) {
|
|
|
|
throw new \Exception('Call "setKey" first!');
|
2016-10-08 15:21:10 +02:00
|
|
|
}
|
2022-07-01 15:10:30 +02:00
|
|
|
|
2019-04-29 20:12:43 +02:00
|
|
|
return hash('md5', $this->key) . '.cache';
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2015-11-05 10:12:58 +00:00
|
|
|
}
|