From 84956c4dafa88f44c98ec162dbfe2495cd524ed8 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 9 Sep 2016 22:14:49 +0200 Subject: [PATCH] [HttpCachingBridgeAbstract] Add duration parameter to 'get_cached' This allows to specify the cache duration for a specific url without the need to delete the cache file first. Instead the cache file is automatically deleted if the duration elapsed. --- lib/HttpCachingBridgeAbstract.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/HttpCachingBridgeAbstract.php b/lib/HttpCachingBridgeAbstract.php index 15cddc5b..a697984f 100644 --- a/lib/HttpCachingBridgeAbstract.php +++ b/lib/HttpCachingBridgeAbstract.php @@ -10,13 +10,20 @@ abstract class HttpCachingBridgeAbstract extends BridgeAbstract { /** * Maintain locally cached versions of pages to download, to avoid multiple downloads. * @param url url to cache + * @param duration duration of the cache file in seconds (default: 24h/86400s) * @return content of the file as string */ - public function get_cached($url){ + public function get_cached($url, $duration = 86400){ // TODO build this from the variable given to Cache $cacheDir = __DIR__ . '/../cache/pages/'; $filepath = $this->buildCacheFilePath($url, $cacheDir); + if(file_exists($filepath) && filectime($filepath) < time() - $duration){ + $this->debugMessage('Cache file ' . $filepath . ' exceeded duration of ' . $duration . ' seconds.'); + unlink ($filepath); + $this->debugMessage('Cached file deleted: ' . $filepath); + } + if(file_exists($filepath)){ $this->debugMessage('loading cached file from ' . $filepath . ' for page at url ' . $url); // TODO touch file and its parent, and try to do neighbour deletion