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 {
|
|
|
|
|
|
|
|
protected $param;
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function loadData(){
|
|
|
|
$datas = unserialize(file_get_contents($this->getCacheFile()));
|
|
|
|
return $datas;
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function saveData($datas){
|
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($datas));
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
if(!$writeStream) {
|
|
|
|
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();
|
|
|
|
if(file_exists($cacheFile)){
|
|
|
|
return filemtime($cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-07 23:33:45 +03:00
|
|
|
public function purgeCache(){
|
|
|
|
$cacheTimeLimit = time() - 86400; // 86400 -> 24h
|
2016-10-07 23:36:36 +03:00
|
|
|
$cachePath = $this->getCachePath();
|
2016-10-07 23:33:45 +03: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()){
|
|
|
|
if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit)
|
|
|
|
unlink($cacheFile->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:04:14 +03:00
|
|
|
/**
|
|
|
|
* Set HTTP GET parameters
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setParameters(array $param){
|
2016-10-08 15:52:03 +03:00
|
|
|
$this->param = $param;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
|
|
|
* Return cache path (and create if not exist)
|
|
|
|
* @return string Cache path
|
|
|
|
*/
|
|
|
|
protected function getCachePath(){
|
|
|
|
$cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ?
|
|
|
|
|
2016-10-07 23:56:10 +03:00
|
|
|
if(!is_dir($cacheDir)){
|
2016-10-08 00:20:32 +03:00
|
|
|
mkdir($cacheDir, 0755, true);
|
|
|
|
chmod($cacheDir, 0755);
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $cacheDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file name use for cache store
|
|
|
|
* @return string Path to the file cache
|
|
|
|
*/
|
|
|
|
protected function getCacheFile(){
|
|
|
|
return $this->getCachePath() . $this->getCacheName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines file name for store the cache
|
|
|
|
* return string
|
|
|
|
*/
|
|
|
|
protected function getCacheName(){
|
2016-10-08 16:21:10 +03:00
|
|
|
if(is_null($this->param)){
|
|
|
|
throw new \Exception('Call "setParameters" first!');
|
|
|
|
}
|
|
|
|
|
2016-10-08 16:17:08 +03:00
|
|
|
return hash('sha1', http_build_query($this->param)) . '.cache';
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2015-11-05 13:12:58 +03:00
|
|
|
}
|