Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup garbage collection for global file cache #14500

Merged
merged 1 commit into from
Feb 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions lib/private/cache/fileglobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,4 @@ public function clear($prefix='') {
}
}
}

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);
}
}
}
}
}
}
}
49 changes: 46 additions & 3 deletions lib/private/cache/fileglobalgc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
73 changes: 73 additions & 0 deletions tests/lib/cache/fileglobalgc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* ownCloud
*
* @author Robin Appelman
* @copyright 2012 Robin Appelman [email protected]
*
* 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));
}
}