From 9ac678aac50845674b8eb836e232fa236a2d8ca5 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 22:33:45 +0200 Subject: [PATCH] [Cache] Move 'purge' function to implementations The purge function is cache specific and thus belongs to the specific implementation. --- caches/FileCache.php | 20 ++++++++++++++++++++ index.php | 4 ++-- lib/Cache.php | 21 --------------------- lib/CacheInterface.php | 1 + 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index eaf16f23..e11ce234 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -34,6 +34,26 @@ class FileCache extends CacheAbstract { return false; } + public function purgeCache(){ + $cacheTimeLimit = time() - 86400; // 86400 -> 24h + $cachePath = 'cache'; + if(file_exists($cachePath)){ + $cacheIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($cachePath), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach($cacheIterator as $cacheFile){ + if(in_array($cacheFile->getBasename(), array('.', '..'))) + continue; + elseif($cacheFile->isFile()){ + if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit) + unlink($cacheFile->getPathname()); + } + } + } + } + /** * Cache is prepared ? * Note : Cache name is based on request information, then cache must be prepare before use diff --git a/index.php b/index.php index d104caa4..43e777fe 100644 --- a/index.php +++ b/index.php @@ -84,8 +84,6 @@ if(!file_exists($whitelist_file)){ $whitelist_selection = explode("\n", file_get_contents($whitelist_file)); } -Cache::purge(); - try { Bridge::setDir(__DIR__ . '/bridges/'); @@ -120,6 +118,8 @@ try { $bridge = Bridge::create($bridge); $cache = Cache::create('FileCache'); + $cache->purgeCache(); + $bridge->setCache($cache); $noproxy = filter_input(INPUT_GET, '_noproxy', FILTER_VALIDATE_BOOLEAN); diff --git a/lib/Cache.php b/lib/Cache.php index 192cb3bf..414e6824 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -50,25 +50,4 @@ class Cache { static public function isValidNameCache($nameCache){ return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache); } - - static public function purge(){ - $cacheTimeLimit = time() - 86400; // 86400 -> 24h - $cachePath = 'cache'; - if(file_exists($cachePath)){ - $cacheIterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($cachePath), - RecursiveIteratorIterator::CHILD_FIRST - ); - - foreach($cacheIterator as $cacheFile){ - if(in_array($cacheFile->getBasename(), array('.', '..'))) - continue; - elseif($cacheFile->isFile()){ - if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit) - unlink($cacheFile->getPathname()); - } - } - } - } - } diff --git a/lib/CacheInterface.php b/lib/CacheInterface.php index 4c59df30..ab1066e3 100644 --- a/lib/CacheInterface.php +++ b/lib/CacheInterface.php @@ -3,4 +3,5 @@ interface CacheInterface { public function loadData(); public function saveData($datas); public function getTime(); + public function purgeCache(); }