2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cache with file system
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
class FileCache extends CacheAbstract {
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function loadData(){
|
|
|
|
$this->isPrepareCache();
|
|
|
|
$datas = unserialize(file_get_contents($this->getCacheFile()));
|
|
|
|
return $datas;
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function saveData($datas){
|
|
|
|
$this->isPrepareCache();
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$writeStream = file_put_contents($this->getCacheFile(), serialize($datas));
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
if(!$writeStream) {
|
|
|
|
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(){
|
|
|
|
$this->isPrepareCache();
|
2015-11-05 11:12:58 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$cacheFile = $this->getCacheFile();
|
|
|
|
if(file_exists($cacheFile)){
|
|
|
|
return filemtime($cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-07 22:33:45 +02:00
|
|
|
public function purgeCache(){
|
|
|
|
$cacheTimeLimit = time() - 86400; // 86400 -> 24h
|
2016-10-07 22:36:36 +02:00
|
|
|
$cachePath = $this->getCachePath();
|
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()){
|
|
|
|
if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit)
|
|
|
|
unlink($cacheFile->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
/**
|
|
|
|
* Cache is prepared ?
|
|
|
|
* Note : Cache name is based on request information, then cache must be prepare before use
|
|
|
|
* @return \Exception|true
|
|
|
|
*/
|
|
|
|
protected function isPrepareCache(){
|
|
|
|
if(is_null($this->param)){
|
|
|
|
throw new \Exception('Please feed "prepare" method before try to load');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return cache path (and create if not exist)
|
|
|
|
* @return string Cache path
|
|
|
|
*/
|
|
|
|
protected function getCachePath(){
|
|
|
|
$cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ?
|
|
|
|
|
2016-10-07 22:56:10 +02:00
|
|
|
if(!is_dir($cacheDir)){
|
2016-10-07 23:20:32 +02:00
|
|
|
mkdir($cacheDir, 0755, true);
|
|
|
|
chmod($cacheDir, 0755);
|
2015-11-05 11:12:58 +01:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02: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(){
|
|
|
|
$this->isPrepareCache();
|
|
|
|
|
|
|
|
$stringToEncode = $_SERVER['REQUEST_URI'] . http_build_query($this->param);
|
|
|
|
$stringToEncode = preg_replace('/(\?|&)format=[^&]*/i', '$1', $stringToEncode);
|
|
|
|
return hash('sha1', $stringToEncode) . '.cache';
|
|
|
|
}
|
2015-11-05 11:12:58 +01:00
|
|
|
}
|