From b6feda2377e2ffe5ae5f9b8812a28a6a0203360d Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 16:30:01 +0200 Subject: [PATCH] [contents] Use FileCache for getSimpleHTMLDOMCached --- lib/contents.php | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/contents.php b/lib/contents.php index 747db5bf..470efeb3 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -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); } }