]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cleanup garbage collection for global file cache
authorRobin Appelman <icewind@owncloud.com>
Wed, 25 Feb 2015 13:13:58 +0000 (14:13 +0100)
committerRobin Appelman <icewind@owncloud.com>
Wed, 25 Feb 2015 13:13:58 +0000 (14:13 +0100)
lib/private/cache/fileglobal.php
lib/private/cache/fileglobalgc.php
tests/lib/cache/fileglobalgc.php [new file with mode: 0644]

index b7ca3f5eb649aaec36921acb376b65c8500bcc09..2605340648f14501199c23a967c7814534d12123 100644 (file)
@@ -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);
-                                               }
-                                       }
-                               }
-                       }
-               }
-       }
 }
index 9be031b2aec245064d4f4a5ae981ab6b65d62199..c95154952de4ed281032e4d36cda4c0a7a29b40e 100644 (file)
  */
 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 (file)
index 0000000..0b0a4cb
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+/**
+ * ownCloud
+ *
+ * @author Robin Appelman
+ * @copyright 2012 Robin Appelman icewind@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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));
+       }
+}