[Pkg-owncloud-commits] [owncloud] 71/95: Cleanup garbage collection for global file cache
David Prévot
taffit at moszumanska.debian.org
Wed Mar 11 15:49:51 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to annotated tag v8.0.1
in repository owncloud.
commit e82f30caaefefd278ca22c344b5283bfdc8c1a2d
Author: Robin Appelman <icewind at owncloud.com>
Date: Wed Feb 25 14:13:58 2015 +0100
Cleanup garbage collection for global file cache
---
lib/private/cache/fileglobal.php | 25 -------------
lib/private/cache/fileglobalgc.php | 49 +++++++++++++++++++++++--
tests/lib/cache/fileglobalgc.php | 73 ++++++++++++++++++++++++++++++++++++++
3 files changed, 119 insertions(+), 28 deletions(-)
diff --git a/lib/private/cache/fileglobal.php b/lib/private/cache/fileglobal.php
index 8406ada..5c4ff60 100644
--- a/lib/private/cache/fileglobal.php
+++ b/lib/private/cache/fileglobal.php
@@ -86,29 +86,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 399dd5e..e170de8 100644
--- a/lib/private/cache/fileglobalgc.php
+++ b/lib/private/cache/fileglobalgc.php
@@ -2,8 +2,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 0000000..0b0a4cb
--- /dev/null
+++ b/tests/lib/cache/fileglobalgc.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * ownCloud
+ *
+ * @author Robin Appelman
+ * @copyright 2012 Robin Appelman icewind at 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));
+ }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git
More information about the Pkg-owncloud-commits
mailing list