Cleanup garbage collection for global file cache

This commit is contained in:
Robin Appelman 2015-02-25 14:13:58 +01:00
Родитель f5b6226732
Коммит a848a6b1dd
3 изменённых файлов: 119 добавлений и 28 удалений

25
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);
}
}
}
}
}
}
}

49
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');
}
}

73
tests/lib/cache/fileglobalgc.php поставляемый Normal file
Просмотреть файл

@ -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));
}
}