diff --git a/lib/private/cache/fileglobal.php b/lib/private/cache/fileglobal.php index b7ca3f5eb64..2605340648f 100644 --- a/lib/private/cache/fileglobal.php +++ b/lib/private/cache/fileglobal.php @@ -103,29 +103,4 @@ class FileGlobal { } } } - - static public function gc() { - $appConfig = \OC::$server->getAppConfig(); - $last_run = $appConfig->getValue('core', 'global_cache_gc_lastrun', 0); - $now = time(); - if (($now - $last_run) < 300) { - // only do cleanup every 5 minutes - return; - } - $appConfig->setValue('core', 'global_cache_gc_lastrun', $now); - $cache_dir = self::getCacheDir(); - if($cache_dir and is_dir($cache_dir)) { - $dh=opendir($cache_dir); - if(is_resource($dh)) { - while (($file = readdir($dh)) !== false) { - if($file!='.' and $file!='..') { - $mtime = filemtime($cache_dir.$file); - if ($mtime < $now) { - unlink($cache_dir.$file); - } - } - } - } - } - } } diff --git a/lib/private/cache/fileglobalgc.php b/lib/private/cache/fileglobalgc.php index 9be031b2aec..c95154952de 100644 --- a/lib/private/cache/fileglobalgc.php +++ b/lib/private/cache/fileglobalgc.php @@ -21,8 +21,51 @@ */ namespace OC\Cache; -class FileGlobalGC extends \OC\BackgroundJob\Job{ - public function run($argument){ - FileGlobal::gc(); +use OC\BackgroundJob\Job; +use OCP\IConfig; + +class FileGlobalGC extends Job { + public function run($argument) { + $this->gc(\OC::$server->getConfig(), $this->getCacheDir()); + } + + protected function getCacheDir() { + return get_temp_dir() . '/owncloud-' . \OC_Util::getInstanceId() . '/'; + } + + /** + * @param string $cacheDir + * @param int $now + * @return string[] + */ + public function getExpiredPaths($cacheDir, $now) { + $files = scandir($cacheDir); + $files = array_filter($files, function ($file) { + return $file != '.' and $file != '..'; + }); + $paths = array_map(function ($file) use ($cacheDir) { + return $cacheDir . $file; + }, $files); + return array_values(array_filter($paths, function ($path) use ($now) { + return is_file($path) and (filemtime($path) < $now); + })); + } + + /** + * @param \OCP\IConfig $config + * @param string $cacheDir + */ + public function gc(IConfig $config, $cacheDir) { + $lastRun = $config->getAppValue('core', 'global_cache_gc_lastrun', 0); + $now = time(); + if (($now - $lastRun) < 300) { + // only do cleanup every 5 minutes + return; + } + $config->setAppValue('core', 'global_cache_gc_lastrun', $now); + if (!is_dir($cacheDir)) { + return; + } + array_walk($this->getExpiredPaths($cacheDir, $now), 'unlink'); } } diff --git a/tests/lib/cache/fileglobalgc.php b/tests/lib/cache/fileglobalgc.php new file mode 100644 index 00000000000..0b0a4cb002d --- /dev/null +++ b/tests/lib/cache/fileglobalgc.php @@ -0,0 +1,73 @@ +. + * + */ + +namespace Test\Cache; + +use Test\TestCase; + +class FileGlobalGC extends TestCase { + /** + * @var string + */ + private $cacheDir; + + /** + * @var \OC\Cache\FileGlobalGC + */ + private $gc; + + public function setUp() { + $this->cacheDir = \OC::$server->getTempManager()->getTemporaryFolder(); + $this->gc = new \OC\Cache\FileGlobalGC(); + } + + private function addCacheFile($name, $expire) { + file_put_contents($this->cacheDir . $name, 'foo'); + touch($this->cacheDir . $name, $expire); + } + + public function testGetExpiredEmpty() { + $this->assertEquals([], $this->gc->getExpiredPaths($this->cacheDir, time())); + } + + public function testGetExpiredNone() { + $time = time(); + $this->addCacheFile('foo', $time + 10); + $this->assertEquals([], $this->gc->getExpiredPaths($this->cacheDir, $time)); + } + + public function testGetExpired() { + $time = time(); + $this->addCacheFile('foo', $time + 10); + $this->addCacheFile('bar', $time); + $this->addCacheFile('bar2', $time - 10); + $this->addCacheFile('asd', $time - 100); + $this->assertEquals([$this->cacheDir . 'asd', $this->cacheDir . 'bar2'], $this->gc->getExpiredPaths($this->cacheDir, $time)); + } + + public function testGetExpiredDirectory() { + $time = time(); + $this->addCacheFile('foo', $time - 10); + mkdir($this->cacheDir . 'asd'); + $this->assertEquals([$this->cacheDir . 'foo'], $this->gc->getExpiredPaths($this->cacheDir, $time)); + } +}