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;
|
2016-10-08 14:52:03 +02:00
|
|
|
protected $param;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function loadData(){
|
2016-10-16 11:08:08 +02:00
|
|
|
return json_decode(file_get_contents($this->getCacheFile()), true);
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function saveData($datas){
|
2016-10-16 11:08:08 +02:00
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), json_encode($datas, JSON_PRETTY_PRINT));
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-11-09 19:10:40 +01:00
|
|
|
if($writeStream === false) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \Exception("Cannot write the cache... Do you have the right permissions ?");
|
|
|
|
}
|
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();
|
|
|
|
if(file_exists($cacheFile)){
|
|
|
|
return filemtime($cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:18:10 +02:00
|
|
|
public function purgeCache($duration){
|
2016-10-08 16:03:08 +02:00
|
|
|
$cachePath = $this->getPath();
|
2016-10-07 22:33:45 +02:00
|
|
|
if(file_exists($cachePath)){
|
|
|
|
$cacheIterator = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator($cachePath),
|
|
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($cacheIterator as $cacheFile){
|
|
|
|
if(in_array($cacheFile->getBasename(), array('.', '..')))
|
|
|
|
continue;
|
|
|
|
elseif($cacheFile->isFile()){
|
2016-10-08 16:18:10 +02:00
|
|
|
if(filemtime($cacheFile->getPathname()) < time() - $duration)
|
2016-10-07 22:33:45 +02:00
|
|
|
unlink($cacheFile->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:03:08 +02:00
|
|
|
/**
|
|
|
|
* Set cache path
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setPath($path){
|
|
|
|
if(is_null($path) || !is_string($path)){
|
|
|
|
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 15:04:14 +02:00
|
|
|
/**
|
|
|
|
* Set HTTP GET parameters
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setParameters(array $param){
|
2016-10-08 15:34:17 +02:00
|
|
|
$this->param = array_map('strtolower', $param);
|
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
|
|
|
|
*/
|
2016-10-08 16:03:08 +02:00
|
|
|
protected function getPath(){
|
|
|
|
if(is_null($this->path)){
|
|
|
|
throw new \Exception('Call "setPath" first!');
|
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
|
|
|
|
*/
|
|
|
|
protected 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
|
|
|
|
*/
|
|
|
|
protected function getCacheName(){
|
2016-10-08 15:21:10 +02:00
|
|
|
if(is_null($this->param)){
|
|
|
|
throw new \Exception('Call "setParameters" first!');
|
|
|
|
}
|
|
|
|
|
2016-10-16 11:08:08 +02:00
|
|
|
return hash('md5', http_build_query($this->param)) . '.cache';
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2015-11-05 11:12:58 +01:00
|
|
|
}
|