[HttpCachingBridgeAbstract] Don't pass cache directory as parameter

This commit is contained in:
logmanoriginal 2016-09-09 22:23:40 +02:00
parent 84956c4daf
commit ce00c6f869
1 changed files with 10 additions and 13 deletions

View File

@ -14,9 +14,7 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
* @return content of the file as string * @return content of the file as string
*/ */
public function get_cached($url, $duration = 86400){ public function get_cached($url, $duration = 86400){
// TODO build this from the variable given to Cache $filepath = $this->buildCacheFilePath($url);
$cacheDir = __DIR__ . '/../cache/pages/';
$filepath = $this->buildCacheFilePath($url, $cacheDir);
if(file_exists($filepath) && filectime($filepath) < time() - $duration){ if(file_exists($filepath) && filectime($filepath) < time() - $duration){
$this->debugMessage('Cache file ' . $filepath . ' exceeded duration of ' . $duration . ' seconds.'); $this->debugMessage('Cache file ' . $filepath . ' exceeded duration of ' . $duration . ' seconds.');
@ -27,7 +25,7 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
if(file_exists($filepath)){ if(file_exists($filepath)){
$this->debugMessage('loading cached file from ' . $filepath . ' for page at url ' . $url); $this->debugMessage('loading cached file from ' . $filepath . ' for page at url ' . $url);
// TODO touch file and its parent, and try to do neighbour deletion // TODO touch file and its parent, and try to do neighbour deletion
$this->refresh_in_cache($cacheDir, $filepath); $this->refresh_in_cache($filepath);
$content = file_get_contents($filepath); $content = file_get_contents($filepath);
} else { } else {
$this->debugMessage('we have no local copy of ' . $url . ' Downloading to ' . $filepath); $this->debugMessage('we have no local copy of ' . $url . ' Downloading to ' . $filepath);
@ -48,9 +46,7 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
} }
public function get_cached_time($url){ public function get_cached_time($url){
// TODO build this from the variable given to Cache $filepath = $this->buildCacheFilePath($url);
$cacheDir = __DIR__ . '/../cache/pages/';
$filepath = $this->buildCacheFilePath($url, $cacheDir);
if(!file_exists($filepath)){ if(!file_exists($filepath)){
$this->get_cached($url); $this->get_cached($url);
@ -59,7 +55,8 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
return filectime($filepath); return filectime($filepath);
} }
private function refresh_in_cache($cacheDir, $filepath){ private function refresh_in_cache($filepath){
$cacheDir = __DIR__ . '/../cache/pages/';
$currentPath = $filepath; $currentPath = $filepath;
while(!$cacheDir == $currentPath){ while(!$cacheDir == $currentPath){
touch($currentPath); touch($currentPath);
@ -67,7 +64,9 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
} }
} }
private function buildCacheFilePath($url, $cacheDir){ private function buildCacheFilePath($url){
$cacheDir = __DIR__ . '/../cache/pages/';
$simplified_url = str_replace( $simplified_url = str_replace(
['http://', 'https://', '?', '&', '='], ['http://', 'https://', '?', '&', '='],
['', '', '/', '/', '/'], ['', '', '/', '/', '/'],
@ -83,13 +82,11 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
$filepath .= 'index.html'; $filepath .= 'index.html';
} }
return $filepath; return realpath($filepath);
} }
public function remove_from_cache($url){ public function remove_from_cache($url){
// TODO build this from the variable given to Cache $filepath = $this->buildCacheFilePath($url);
$cacheDir = __DIR__ . '/../cache/pages/';
$filepath = $this->buildCacheFilePath($url, $cacheDir);
$this->debugMessage('removing from cache \'' . $filepath . '\''); $this->debugMessage('removing from cache \'' . $filepath . '\'');
unlink($filepath); unlink($filepath);
} }