forked from blallo/rss-bridge
[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.
This commit is contained in:
parent
9711e87fc9
commit
84956c4daf
1 changed files with 8 additions and 1 deletions
|
@ -10,13 +10,20 @@ 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
|
||||||
|
* @param duration duration of the cache file in seconds (default: 24h/86400s)
|
||||||
* @return content of the file as string
|
* @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
|
// TODO build this from the variable given to Cache
|
||||||
$cacheDir = __DIR__ . '/../cache/pages/';
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
||||||
$filepath = $this->buildCacheFilePath($url, $cacheDir);
|
$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)){
|
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
|
||||||
|
|
Loading…
Reference in a new issue