2013-04-21 01:27:46 +04:00
|
|
|
<?php
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-17 19:46:33 +04:00
|
|
|
namespace OC\Cache;
|
2013-04-21 01:27:46 +04:00
|
|
|
|
2015-02-25 16:13:58 +03:00
|
|
|
use OC\BackgroundJob\Job;
|
|
|
|
use OCP\IConfig;
|
|
|
|
|
|
|
|
class FileGlobalGC extends Job {
|
2015-03-18 19:19:04 +03:00
|
|
|
// only do cleanup every 5 minutes
|
|
|
|
const CLEANUP_TTL_SEC = 300;
|
|
|
|
|
2015-02-25 16:13:58 +03:00
|
|
|
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();
|
2015-03-18 19:19:04 +03:00
|
|
|
if (($now - $lastRun) < self::CLEANUP_TTL_SEC) {
|
2015-02-25 16:13:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$config->setAppValue('core', 'global_cache_gc_lastrun', $now);
|
|
|
|
if (!is_dir($cacheDir)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-26 18:42:41 +03:00
|
|
|
$paths = $this->getExpiredPaths($cacheDir, $now);
|
2015-03-18 03:07:18 +03:00
|
|
|
array_walk($paths, function($file) {
|
|
|
|
unlink($file);
|
|
|
|
});
|
2013-04-21 01:27:46 +04:00
|
|
|
}
|
|
|
|
}
|