[contents] Use FileCache for getSimpleHTMLDOMCached

This commit is contained in:
logmanoriginal 2016-10-08 16:30:01 +02:00
parent 8fb4db8914
commit b6feda2377
1 changed files with 14 additions and 20 deletions

View File

@ -103,30 +103,24 @@ function getSimpleHTMLDOMCached($url
){
debugMessage('Caching url ' . $url . ', duration ' . $duration);
$filepath = __DIR__ . '/../cache/pages/' . sha1($url) . '.cache';
debugMessage('Cache file ' . $filepath);
// Initialize cache
$cache = Cache::create('FileCache');
$cache->setPath(CACHE_DIR . '/pages');
$cache->purgeCache(86400); // 24 hours (forced)
if(file_exists($filepath) && filectime($filepath) < time() - $duration){
unlink ($filepath);
debugMessage('Cached file deleted: ' . $filepath);
}
if(file_exists($filepath)){
debugMessage('Loading cached file ' . $filepath);
touch($filepath);
$content = file_get_contents($filepath);
} else {
debugMessage('Caching ' . $url . ' to ' . $filepath);
$dir = substr($filepath, 0, strrpos($filepath, '/'));
if(!is_dir($dir)){
debugMessage('Creating directory ' . $dir);
mkdir($dir, 0777, true);
}
$params = [$url];
$cache->setParameters($params);
// Determine if cached file is within duration
$time = $cache->getTime();
if($time !== false
&& (time() - $duration < $time)
&& (!defined('DEBUG') || DEBUG !== true)){ // Contents within duration
$content = $cache->loadData();
} else { // Content not within duration
$content = getContents($url, $use_include_path, $context, $offset, $maxLen);
if($content !== false){
file_put_contents($filepath, $content);
$cache->saveData($content);
}
}