[HttpCachingBridgeAbstract] General cleanup

- Remove buildCacheFilePath -> Single use, so no real purpose
- Simplify debug messages
- Cleanup documentation
This commit is contained in:
logmanoriginal 2016-09-10 00:34:25 +02:00
parent 3a94956915
commit 33584b8423

View file

@ -2,11 +2,8 @@
require_once(__DIR__ . '/BridgeInterface.php'); require_once(__DIR__ . '/BridgeInterface.php');
/** /**
* Extension of BridgeAbstract allowing caching of files downloaded over http. * Extension of BridgeAbstract allowing caching of files downloaded over http.
* TODO allow file cache invalidation by touching files on access, and removing
* files/directories which have not been touched since ... a long time
*/ */
abstract class HttpCachingBridgeAbstract extends BridgeAbstract { abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
/** /**
* Maintain locally cached versions of pages to download, to avoid multiple downloads. * Maintain locally cached versions of pages to download, to avoid multiple downloads.
* @param url url to cache * @param url url to cache
@ -14,25 +11,26 @@ 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){
$filepath = $this->buildCacheFilePath($url); $this->debugMessage('Caching url ' . $url . ', duration ' . $duration);
$filepath = __DIR__ . '/../cache/pages/' . sha1($url) . '.cache';
$this->debugMessage('Cache file ' . $filepath);
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.');
unlink ($filepath); unlink ($filepath);
$this->debugMessage('Cached file deleted: ' . $filepath); $this->debugMessage('Cached file deleted: ' . $filepath);
} }
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 ' . $filepath);
// TODO touch file and its parent, and try to do neighbour deletion
touch($filepath); touch($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('Caching ' . $url . ' to ' . $filepath);
$dir = substr($filepath, 0, strrpos($filepath, '/')); $dir = substr($filepath, 0, strrpos($filepath, '/'));
if(!is_dir($dir)){ if(!is_dir($dir)){
$this->debugMessage('creating directories for ' . $dir); $this->debugMessage('Creating directory ' . $dir);
mkdir($dir, 0777, true); mkdir($dir, 0777, true);
} }
@ -44,8 +42,4 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
return str_get_html($content); return str_get_html($content);
} }
private function buildCacheFilePath($url){
return __DIR__ . '/../cache/pages/' . sha1($url) . '.cache';
}
} }