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