2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cache with file system
|
|
|
|
*/
|
2016-10-08 15:52:03 +03:00
|
|
|
class FileCache implements CacheInterface {
|
2016-10-08 17:03:08 +03:00
|
|
|
protected $path;
|
2019-04-29 21:12:43 +03:00
|
|
|
protected $key;
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2022-03-24 23:29:16 +03:00
|
|
|
public function __construct() {
|
|
|
|
if (!is_writable(PATH_CACHE)) {
|
|
|
|
returnServerError(
|
|
|
|
'RSS-Bridge does not have write permissions for '
|
|
|
|
. PATH_CACHE . '!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function loadData(){
|
2018-05-05 20:05:48 +03:00
|
|
|
if(file_exists($this->getCacheFile())) {
|
|
|
|
return unserialize(file_get_contents($this->getCacheFile()));
|
|
|
|
}
|
2019-04-29 21:12:43 +03:00
|
|
|
|
|
|
|
return null;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function saveData($data){
|
2017-02-18 14:54:26 +03:00
|
|
|
// Notice: We use plain serialize() here to reduce memory footprint on
|
|
|
|
// large input data.
|
2019-04-29 21:12:43 +03:00
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($data));
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-11-09 21:10:40 +03:00
|
|
|
if($writeStream === false) {
|
2018-06-30 00:55:33 +03:00
|
|
|
throw new \Exception('Cannot write the cache... Do you have the right permissions ?');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function getTime(){
|
|
|
|
$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);
|
|
|
|
return ($time !== false) ? $time : null;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
return null;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
public function purgeCache($seconds){
|
2016-10-08 17:03:08 +03:00
|
|
|
$cachePath = $this->getPath();
|
2017-07-29 20:28:00 +03:00
|
|
|
if(file_exists($cachePath)) {
|
2016-10-07 23:33:45 +03:00
|
|
|
$cacheIterator = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator($cachePath),
|
|
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
|
|
);
|
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach($cacheIterator as $cacheFile) {
|
2017-02-18 12:23:46 +03:00
|
|
|
if(in_array($cacheFile->getBasename(), array('.', '..', '.gitkeep')))
|
2016-10-07 23:33:45 +03:00
|
|
|
continue;
|
2017-07-29 20:28:00 +03:00
|
|
|
elseif($cacheFile->isFile()) {
|
2019-04-29 21:12:43 +03:00
|
|
|
if(filemtime($cacheFile->getPathname()) < time() - $seconds)
|
2016-10-07 23:33:45 +03:00
|
|
|
unlink($cacheFile->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 17:03:08 +03:00
|
|
|
/**
|
2019-04-29 21:12:43 +03:00
|
|
|
* Set scope
|
2016-10-08 17:03:08 +03:00
|
|
|
* @return self
|
|
|
|
*/
|
2019-04-29 21:12:43 +03:00
|
|
|
public function setScope($scope){
|
|
|
|
if(is_null($scope) || !is_string($scope)) {
|
|
|
|
throw new \Exception('The given scope is invalid!');
|
2016-10-08 17:03:08 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
$this->path = PATH_CACHE . trim($scope, " \t\n\r\0\x0B\\\/") . '/';
|
2016-10-08 17:03:08 +03:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:04:14 +03:00
|
|
|
/**
|
2019-04-29 21:12:43 +03:00
|
|
|
* Set key
|
2016-10-08 16:04:14 +03:00
|
|
|
* @return self
|
|
|
|
*/
|
2019-04-29 21:12:43 +03:00
|
|
|
public function setKey($key){
|
|
|
|
if (!empty($key) && is_array($key)) {
|
|
|
|
$key = array_map('strtolower', $key);
|
|
|
|
}
|
|
|
|
$key = json_encode($key);
|
2016-10-08 15:52:03 +03:00
|
|
|
|
2019-04-29 21:12:43 +03:00
|
|
|
if (!is_string($key)) {
|
|
|
|
throw new \Exception('The given key is invalid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->key = $key;
|
2016-10-08 15:52:03 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
|
|
|
* Return cache path (and create if not exist)
|
|
|
|
* @return string Cache path
|
|
|
|
*/
|
2019-04-29 21:12:43 +03:00
|
|
|
private function getPath(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($this->path)) {
|
2019-04-29 21:12:43 +03:00
|
|
|
throw new \Exception('Call "setScope" first!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_dir($this->path)) {
|
|
|
|
if (mkdir($this->path, 0755, true) !== true) {
|
|
|
|
throw new \Exception('Unable to create ' . $this->path);
|
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-10-08 17:03:08 +03:00
|
|
|
return $this->path;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file name use for cache store
|
|
|
|
* @return string Path to the file cache
|
|
|
|
*/
|
2019-04-29 21:12:43 +03:00
|
|
|
private function getCacheFile(){
|
2016-10-08 17:03:08 +03:00
|
|
|
return $this->getPath() . $this->getCacheName();
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines file name for store the cache
|
|
|
|
* return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|