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;
|
2016-10-08 15:52:03 +03:00
|
|
|
protected $param;
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function loadData(){
|
2017-02-18 14:54:26 +03:00
|
|
|
return unserialize(file_get_contents($this->getCacheFile()));
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function saveData($datas){
|
2017-02-18 14:54:26 +03:00
|
|
|
// Notice: We use plain serialize() here to reduce memory footprint on
|
|
|
|
// large input data.
|
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($datas));
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-11-09 21:10:40 +03:00
|
|
|
if($writeStream === false) {
|
2016-09-10 21:41:11 +03:00
|
|
|
throw new \Exception("Cannot write the cache... Do you have the right permissions ?");
|
|
|
|
}
|
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();
|
2017-07-29 20:28:00 +03:00
|
|
|
if(file_exists($cacheFile)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
return filemtime($cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-08 17:18:10 +03:00
|
|
|
public function purgeCache($duration){
|
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()) {
|
2016-10-08 17:18:10 +03:00
|
|
|
if(filemtime($cacheFile->getPathname()) < time() - $duration)
|
2016-10-07 23:33:45 +03:00
|
|
|
unlink($cacheFile->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 17:03:08 +03:00
|
|
|
/**
|
|
|
|
* Set cache path
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setPath($path){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($path) || !is_string($path)) {
|
2016-10-08 17:03:08 +03:00
|
|
|
throw new \Exception('The given path is invalid!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->path = $path;
|
|
|
|
|
|
|
|
// Make sure path ends with '/' or '\'
|
|
|
|
$lastchar = substr($this->path, -1, 1);
|
|
|
|
if($lastchar !== '/' && $lastchar !== '\\')
|
|
|
|
$this->path .= '/';
|
|
|
|
|
|
|
|
if(!is_dir($this->path))
|
|
|
|
mkdir($this->path, 0755, true);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:04:14 +03:00
|
|
|
/**
|
|
|
|
* Set HTTP GET parameters
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setParameters(array $param){
|
2016-10-08 16:34:17 +03:00
|
|
|
$this->param = array_map('strtolower', $param);
|
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
|
|
|
|
*/
|
2016-10-08 17:03:08 +03:00
|
|
|
protected function getPath(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($this->path)) {
|
2016-10-08 17:03:08 +03:00
|
|
|
throw new \Exception('Call "setPath" first!');
|
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
|
|
|
|
*/
|
|
|
|
protected 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
|
|
|
|
*/
|
|
|
|
protected function getCacheName(){
|
2017-07-29 20:28:00 +03:00
|
|
|
if(is_null($this->param)) {
|
2016-10-08 16:21:10 +03:00
|
|
|
throw new \Exception('Call "setParameters" first!');
|
|
|
|
}
|
|
|
|
|
2017-02-18 14:54:26 +03:00
|
|
|
// Change character when making incompatible changes to prevent loading
|
|
|
|
// errors due to incompatible file contents \|/
|
|
|
|
return hash('md5', http_build_query($this->param) . 'A') . '.cache';
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|