summaryrefslogtreecommitdiffstats
path: root/lib/private/cache
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-02-25 14:13:58 +0100
committerRobin Appelman <icewind@owncloud.com>2015-02-25 14:13:58 +0100
commita848a6b1dd59b124318e1a754a1d51e3189949aa (patch)
treef0e8d0d2ca06f8da22182c4f7e05d48ea627e127 /lib/private/cache
parentf5b62267325415b307cf2d47b69d11d4337536e4 (diff)
downloadnextcloud-server-a848a6b1dd59b124318e1a754a1d51e3189949aa.tar.gz
nextcloud-server-a848a6b1dd59b124318e1a754a1d51e3189949aa.zip
Cleanup garbage collection for global file cache
Diffstat (limited to 'lib/private/cache')
-rw-r--r--lib/private/cache/fileglobal.php25
-rw-r--r--lib/private/cache/fileglobalgc.php49
2 files changed, 46 insertions, 28 deletions
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');
}
}