Merge pull request #16200 from owncloud/kill-globalfilecache

Drop file caching
This commit is contained in:
Morris Jobke 2015-06-02 09:11:33 +02:00
commit cf2c599218
16 changed files with 144 additions and 946 deletions

View File

@ -37,12 +37,18 @@ abstract class Proxy {
static private $accesses = array();
private $ldap = null;
/** @var \OCP\ICache|null */
private $cache;
/**
* @param ILDAPWrapper $ldap
*/
public function __construct(ILDAPWrapper $ldap) {
$this->ldap = $ldap;
$this->cache = \OC\Cache::getGlobalCache();
$memcache = \OC::$server->getMemCacheFactory();
if($memcache->isAvailable()) {
$this->cache = $memcache->create();
}
}
/**
@ -151,7 +157,7 @@ abstract class Proxy {
* @return mixed|null
*/
public function getFromCache($key) {
if(!$this->isCached($key)) {
if(is_null($this->cache) || !$this->isCached($key)) {
return null;
}
$key = $this->getCacheKey($key);
@ -164,6 +170,9 @@ abstract class Proxy {
* @return bool
*/
public function isCached($key) {
if(is_null($this->cache)) {
return false;
}
$key = $this->getCacheKey($key);
return $this->cache->hasKey($key);
}
@ -173,12 +182,18 @@ abstract class Proxy {
* @param mixed $value
*/
public function writeToCache($key, $value) {
if(is_null($this->cache)) {
return;
}
$key = $this->getCacheKey($key);
$value = base64_encode(serialize($value));
$this->cache->set($key, $value, '2592000');
}
public function clearCache() {
if(is_null($this->cache)) {
return;
}
$this->cache->clear($this->getCacheKey(null));
}
}

View File

@ -652,7 +652,6 @@ class OC {
OC_User::setupBackends();
}
self::registerCacheHooks();
self::registerFilesystemHooks();
if (\OC::$server->getSystemConfig()->getValue('enable_previews', true)) {
self::registerPreviewHooks();
@ -734,19 +733,6 @@ class OC {
}
}
/**
* register hooks for the cache
*/
public static function registerCacheHooks() {
if (\OC::$server->getSystemConfig()->getValue('installed', false) && !\OCP\Util::needUpgrade()) { //don't try to do this before we are properly setup
\OCP\BackgroundJob::registerJob('OC\Cache\FileGlobalGC');
// NOTE: This will be replaced to use OCP
$userSession = \OC_User::getUserSession();
$userSession->listen('postLogin', '\OC\Cache\File', 'loginListener');
}
}
/**
* register hooks for the cache
*/

View File

@ -172,9 +172,6 @@ class JobList implements IJobList {
/**
* @var Job $job
*/
if ($class === 'OC_Cache_FileGlobalGC') {
$class = '\OC\Cache\FileGlobalGC';
}
if (!class_exists($class)) {
// job from disabled app or old version of an app, no need to do anything
return null;

View File

@ -1,129 +0,0 @@
<?php
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC;
class Cache {
/**
* @var Cache $user_cache
*/
static protected $user_cache;
/**
* @var Cache $global_cache
*/
static protected $global_cache;
/**
* get the global cache
* @return Cache
*/
static public function getGlobalCache() {
if (!self::$global_cache) {
self::$global_cache = new Cache\FileGlobal();
}
return self::$global_cache;
}
/**
* get the user cache
* @return Cache
*/
static public function getUserCache() {
if (!self::$user_cache) {
self::$user_cache = new Cache\File();
}
return self::$user_cache;
}
/**
* get a value from the user cache
* @param string $key
* @return mixed
*/
static public function get($key) {
$user_cache = self::getUserCache();
return $user_cache->get($key);
}
/**
* set a value in the user cache
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
static public function set($key, $value, $ttl=0) {
if (empty($key)) {
return false;
}
$user_cache = self::getUserCache();
return $user_cache->set($key, $value, $ttl);
}
/**
* check if a value is set in the user cache
* @param string $key
* @return bool
*/
static public function hasKey($key) {
$user_cache = self::getUserCache();
return $user_cache->hasKey($key);
}
/**
* remove an item from the user cache
* @param string $key
* @return bool
*/
static public function remove($key) {
$user_cache = self::getUserCache();
return $user_cache->remove($key);
}
/**
* clear the user cache of all entries starting with a prefix
* @param string $prefix (optional)
* @return bool
*/
static public function clear($prefix='') {
$user_cache = self::getUserCache();
return $user_cache->clear($prefix);
}
/**
* creates cache key based on the files given
* @param string[] $files
* @return string
*/
static public function generateCacheKeyFromFiles($files) {
$key = '';
sort($files);
foreach($files as $file) {
$stat = stat($file);
$key .= $file.$stat['mtime'].$stat['size'];
}
return md5($key);
}
}

View File

@ -1,80 +0,0 @@
<?php
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Cache;
class Broker {
/**
* @var \OC\Cache
*/
protected $fast_cache;
/**
* @var \OC\Cache
*/
protected $slow_cache;
public function __construct($fast_cache, $slow_cache) {
$this->fast_cache = $fast_cache;
$this->slow_cache = $slow_cache;
}
public function get($key) {
if ($r = $this->fast_cache->get($key)) {
return $r;
}
return $this->slow_cache->get($key);
}
public function set($key, $value, $ttl=0) {
if (!$this->fast_cache->set($key, $value, $ttl)) {
if ($this->fast_cache->hasKey($key)) {
$this->fast_cache->remove($key);
}
return $this->slow_cache->set($key, $value, $ttl);
}
return true;
}
public function hasKey($key) {
if ($this->fast_cache->hasKey($key)) {
return true;
}
return $this->slow_cache->hasKey($key);
}
public function remove($key) {
if ($this->fast_cache->remove($key)) {
return true;
}
return $this->slow_cache->remove($key);
}
public function clear($prefix='') {
$this->fast_cache->clear($prefix);
$this->slow_cache->clear($prefix);
}
}

View File

@ -1,174 +0,0 @@
<?php
/**
* @author Arthur Schiwon <blizzz@owncloud.com>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Björn Schießle <schiessle@owncloud.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Michael Gapczynski <GapczynskiM@gmail.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Cache;
use OC\Files\Filesystem;
use OC\Files\View;
use OCP\Security\ISecureRandom;
class File {
protected $storage;
/**
* Returns the cache storage for the logged in user
*
* @return \OC\Files\View cache storage
*/
protected function getStorage() {
if (isset($this->storage)) {
return $this->storage;
}
if (\OC_User::isLoggedIn()) {
$rootView = new View();
$user = \OC::$server->getUserSession()->getUser();
Filesystem::initMountPoints($user->getUID());
if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
$rootView->mkdir('/' . $user->getUID() . '/cache');
}
$this->storage = new View('/' . $user->getUID() . '/cache');
return $this->storage;
} else {
\OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
}
}
/**
* @param string $key
*/
public function get($key) {
$result = null;
if ($this->hasKey($key)) {
$storage = $this->getStorage();
$result = $storage->file_get_contents($key);
}
return $result;
}
/**
* Returns the size of the stored/cached data
*
* @param string $key
* @return int
*/
public function size($key) {
$result = 0;
if ($this->hasKey($key)) {
$storage = $this->getStorage();
$result = $storage->filesize($key);
}
return $result;
}
/**
* @param string $key
*/
public function set($key, $value, $ttl = 0) {
$storage = $this->getStorage();
$result = false;
// unique id to avoid chunk collision, just in case
$uniqueId = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(
16,
ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
);
// use part file to prevent hasKey() to find the key
// while it is being written
$keyPart = $key . '.' . $uniqueId . '.part';
if ($storage and $storage->file_put_contents($keyPart, $value)) {
if ($ttl === 0) {
$ttl = 86400; // 60*60*24
}
$result = $storage->touch($keyPart, time() + $ttl);
$result &= $storage->rename($keyPart, $key);
}
return $result;
}
public function hasKey($key) {
$storage = $this->getStorage();
if ($storage && $storage->is_file($key) && $storage->isReadable($key)) {
return true;
}
return false;
}
/**
* @param string $key
*/
public function remove($key) {
$storage = $this->getStorage();
if (!$storage) {
return false;
}
return $storage->unlink($key);
}
public function clear($prefix = '') {
$storage = $this->getStorage();
if ($storage and $storage->is_dir('/')) {
$dh = $storage->opendir('/');
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) {
$storage->unlink('/' . $file);
}
}
}
}
return true;
}
public function gc() {
$storage = $this->getStorage();
if ($storage and $storage->is_dir('/')) {
$now = time();
$dh = $storage->opendir('/');
if (!is_resource($dh)) {
return null;
}
while (($file = readdir($dh)) !== false) {
if ($file != '.' and $file != '..') {
$mtime = $storage->filemtime('/' . $file);
if ($mtime < $now) {
$storage->unlink('/' . $file);
}
}
}
}
}
public static function loginListener() {
$c = new self();
$c->gc();
}
}

View File

@ -1,107 +0,0 @@
<?php
/**
* @author Arthur Schiwon <blizzz@owncloud.com>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Cache;
class FileGlobal {
static protected function getCacheDir() {
$cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
if (!is_dir($cache_dir)) {
mkdir($cache_dir);
}
return $cache_dir;
}
protected function fixKey($key) {
return str_replace('/', '_', $key);
}
/**
* @param string $key
*/
public function get($key) {
$key = $this->fixKey($key);
if ($this->hasKey($key)) {
$cache_dir = self::getCacheDir();
return file_get_contents($cache_dir.$key);
}
return null;
}
/**
* @param string $key
* @param string $value
*/
public function set($key, $value, $ttl=0) {
$key = $this->fixKey($key);
$cache_dir = self::getCacheDir();
if ($cache_dir and file_put_contents($cache_dir.$key, $value)) {
if ($ttl === 0) {
$ttl = 86400; // 60*60*24
}
return touch($cache_dir.$key, time() + $ttl);
}
return false;
}
public function hasKey($key) {
$key = $this->fixKey($key);
$cache_dir = self::getCacheDir();
if ($cache_dir && is_file($cache_dir.$key) && is_readable($cache_dir.$key)) {
$mtime = filemtime($cache_dir.$key);
if ($mtime < time()) {
unlink($cache_dir.$key);
return false;
}
return true;
}
return false;
}
public function remove($key) {
$cache_dir = self::getCacheDir();
if(!$cache_dir) {
return false;
}
$key = $this->fixKey($key);
return unlink($cache_dir.$key);
}
public function clear($prefix='') {
$cache_dir = self::getCacheDir();
$prefix = $this->fixKey($prefix);
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!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
unlink($cache_dir.$file);
}
}
}
}
}
}

View File

@ -1,80 +0,0 @@
<?php
/**
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Cache;
use OC\BackgroundJob\Job;
use OCP\IConfig;
class FileGlobalGC extends Job {
// only do cleanup every 5 minutes
const CLEANUP_TTL_SEC = 300;
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) < self::CLEANUP_TTL_SEC) {
return;
}
$config->setAppValue('core', 'global_cache_gc_lastrun', $now);
if (!is_dir($cacheDir)) {
return;
}
$paths = $this->getExpiredPaths($cacheDir, $now);
array_walk($paths, function($file) {
if (file_exists($file)) {
unlink($file);
}
});
}
}

View File

@ -1,93 +0,0 @@
<?php
/**
* @author Morris Jobke <hey@morrisjobke.de>
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Cache;
/**
* This interface defines method for accessing the file based user cache.
*/
class UserCache implements \OCP\ICache {
/**
* @var \OC\Cache\File $userCache
*/
protected $userCache;
public function __construct() {
$this->userCache = new File();
}
/**
* Get a value from the user cache
*
* @param string $key
* @return mixed
*/
public function get($key) {
return $this->userCache->get($key);
}
/**
* Set a value in the user cache
*
* @param string $key
* @param string $value
* @param int $ttl Time To Live in seconds. Defaults to 60*60*24
* @return bool
*/
public function set($key, $value, $ttl = 0) {
if (empty($key)) {
return false;
}
return $this->userCache->set($key, $value, $ttl);
}
/**
* Check if a value is set in the user cache
*
* @param string $key
* @return bool
*/
public function hasKey($key) {
return $this->userCache->hasKey($key);
}
/**
* Remove an item from the user cache
*
* @param string $key
* @return bool
*/
public function remove($key) {
return $this->userCache->remove($key);
}
/**
* clear the user cache of all entries starting with a prefix
* @param string $prefix (optional)
* @return bool
*/
public function clear($prefix = '') {
return $this->userCache->clear($prefix);
}
}

View File

@ -32,6 +32,7 @@ use OC\Hooks\Emitter;
use OC\Repair\AssetCache;
use OC\Repair\CleanTags;
use OC\Repair\Collation;
use OC\Repair\DropOldJobs;
use OC\Repair\SqliteAutoincrement;
use OC\Repair\DropOldTables;
use OC\Repair\FillETags;
@ -106,6 +107,7 @@ class Repair extends BasicEmitter {
new FillETags(\OC_DB::getConnection()),
new CleanTags(\OC_DB::getConnection()),
new DropOldTables(\OC_DB::getConnection()),
new DropOldJobs(\OC::$server->getJobList()),
);
}

View File

@ -38,7 +38,6 @@ use bantu\IniGetWrapper\IniGetWrapper;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Db\Db;
use OC\AppFramework\Utility\SimpleContainer;
use OC\Cache\UserCache;
use OC\Command\AsyncBus;
use OC\Diagnostics\NullQueryLogger;
use OC\Diagnostics\EventLogger;
@ -47,6 +46,7 @@ use OC\Lock\MemcacheLockingProvider;
use OC\Lock\NoopLockingProvider;
use OC\Mail\Mailer;
use OC\Memcache\ArrayCache;
use OC\Memcache\Null as NullCache;
use OC\Http\Client\ClientService;
use OC\Security\CertificateManager;
use OC\Files\Node\Root;
@ -219,8 +219,8 @@ class Server extends SimpleContainer implements IServerContainer {
$this->registerService('AppHelper', function ($c) {
return new \OC\AppHelper();
});
$this->registerService('UserCache', function ($c) {
return new UserCache();
$this->registerService('NullCache', function ($c) {
return new NullCache();
});
$this->registerService('MemCacheFactory', function (Server $c) {
$config = $c->getConfig();
@ -661,12 +661,14 @@ class Server extends SimpleContainer implements IServerContainer {
}
/**
* Returns an ICache instance
* Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
* getMemCacheFactory() instead.
*
* @return \OCP\ICache
* @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
*/
public function getCache() {
return $this->query('UserCache');
return $this->query('NullCache');
}
/**

View File

@ -0,0 +1,78 @@
<?php
/**
* @author Arthur Schiwon <blizzz@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Repair;
use OC\Hooks\BasicEmitter;
use OC\RepairStep;
use OCP\BackgroundJob\IJobList;
class DropOldJobs extends BasicEmitter implements RepairStep {
/** @var IJobList */
protected $jobList;
/**
* @param IJobList $jobList
*/
public function __construct(IJobList $jobList) {
$this->jobList = $jobList;
}
/**
* Returns the step's name
*
* @return string
*/
public function getName() {
return 'Drop old background jobs';
}
/**
* Run repair step.
* Must throw exception on error.
*
* @throws \Exception in case of failure
*/
public function run() {
$oldJobs = $this->oldJobs();
foreach($oldJobs as $job) {
if($this->jobList->has($job['class'], $job['arguments'])) {
$this->jobList->remove($job['class'], $job['arguments']);
}
}
}
/**
* returns a list of old jobs as an associative array with keys 'class' and
* 'arguments'.
*
* @return array
*/
public function oldJobs() {
return [
['class' => 'OC_Cache_FileGlobalGC', 'arguments' => null],
['class' => 'OC\Cache\FileGlobalGC', 'arguments' => null],
];
}
}

View File

@ -1,78 +0,0 @@
<?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;
class FileCache extends \Test_Cache {
/** @var string */
private $user;
/** @var string */
private $datadir;
/** @var \OC\Files\Storage\Storage */
private $storage;
function skip() {
//$this->skipUnless(OC_User::isLoggedIn());
}
protected function setUp() {
parent::setUp();
//clear all proxies and hooks so we can do clean testing
\OC_Hook::clear('OC_Filesystem');
//set up temporary storage
$this->storage = \OC\Files\Filesystem::getStorage('/');
\OC\Files\Filesystem::clearMounts();
$storage = new \OC\Files\Storage\Temporary(array());
\OC\Files\Filesystem::mount($storage,array(),'/');
$datadir = str_replace('local::', '', $storage->getId());
$this->datadir = \OC_Config::getValue('cachedirectory', \OC::$SERVERROOT.'/data/cache');
\OC_Config::setValue('cachedirectory', $datadir);
\OC_User::clearBackends();
\OC_User::useBackend(new \OC_User_Dummy());
//login
\OC_User::createUser('test', 'test');
$this->user = \OC_User::getUser();
\OC_User::setUserId('test');
//set up the users dir
$rootView = new \OC\Files\View('');
$rootView->mkdir('/test');
$this->instance=new \OC\Cache\File();
}
protected function tearDown() {
\OC_User::setUserId($this->user);
\OC_Config::setValue('cachedirectory', $this->datadir);
// Restore the original mount point
\OC\Files\Filesystem::clearMounts();
\OC\Files\Filesystem::mount($this->storage, array(), '/');
parent::tearDown();
}
}

View File

@ -1,107 +0,0 @@
<?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));
}
public function testGcUnlink() {
$time = time();
$this->addCacheFile('foo', $time - 10);
$this->addCacheFile('bar', $time - 10);
$this->addCacheFile('asd', $time + 10);
$config = $this->getMock('\OCP\IConfig');
$config->expects($this->once())
->method('getAppValue')
->with('core', 'global_cache_gc_lastrun', 0)
->willReturn($time - \OC\Cache\FileGlobalGC::CLEANUP_TTL_SEC - 1);
$config->expects($this->once())
->method('setAppValue');
$this->gc->gc($config, $this->cacheDir);
$this->assertFileNotExists($this->cacheDir . 'foo');
$this->assertFileNotExists($this->cacheDir . 'bar');
$this->assertFileExists($this->cacheDir . 'asd');
}
public function testGcLastRun() {
$time = time();
$config = $this->getMock('\OCP\IConfig');
$config->expects($this->once())
->method('getAppValue')
->with('core', 'global_cache_gc_lastrun', 0)
->willReturn($time);
$config->expects($this->never())
->method('setAppValue');
$this->gc->gc($config, $this->cacheDir);
}
}

View File

@ -1,74 +0,0 @@
<?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;
class UserCache extends \Test_Cache {
/** @var string */
private $user;
/** @var string */
private $datadir;
/** @var \OC\Files\Storage\Storage */
private $storage;
protected function setUp() {
parent::setUp();
//clear all proxies and hooks so we can do clean testing
\OC_Hook::clear('OC_Filesystem');
//set up temporary storage
$this->storage = \OC\Files\Filesystem::getStorage('/');
\OC\Files\Filesystem::clearMounts();
$storage = new \OC\Files\Storage\Temporary(array());
\OC\Files\Filesystem::mount($storage,array(),'/');
$datadir = str_replace('local::', '', $storage->getId());
$this->datadir = \OC_Config::getValue('cachedirectory', \OC::$SERVERROOT.'/data/cache');
\OC_Config::setValue('cachedirectory', $datadir);
\OC_User::clearBackends();
\OC_User::useBackend(new \OC_User_Dummy());
//login
\OC_User::createUser('test', 'test');
$this->user = \OC_User::getUser();
\OC_User::setUserId('test');
//set up the users dir
$rootView=new \OC\Files\View('');
$rootView->mkdir('/test');
$this->instance=new \OC\Cache\UserCache();
}
protected function tearDown() {
\OC_User::setUserId($this->user);
\OC_Config::setValue('cachedirectory', $this->datadir);
// Restore the original mount point
\OC\Files\Filesystem::clearMounts();
\OC\Files\Filesystem::mount($this->storage, array(), '/');
parent::tearDown();
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
* Copyright (c) 2015 Arthur Schiwon <blizzz@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Repair;
use OCP\BackgroundJob\IJobList;
/**
* Tests for the dropping old tables
*
* @see \OC\Repair\DropOldTables
*/
class DropOldJobs extends \Test\TestCase {
/** @var IJobList */
protected $jobList;
protected function setUp() {
parent::setUp();
$this->jobList = \OC::$server->getJobList();
$this->jobList->add('OC\Cache\FileGlobalGC');
$this->jobList->add('OC_Cache_FileGlobalGC');
}
public function testRun() {
$this->assertTrue($this->jobList->has('OC\Cache\FileGlobalGC', null), 'Asserting that the job OC\Cache\FileGlobalGC exists before repairing');
$this->assertTrue($this->jobList->has('OC_Cache_FileGlobalGC', null), 'Asserting that the job OC_Cache_FileGlobalGC exists before repairing');
$repair = new \OC\Repair\DropOldJobs($this->jobList);
$repair->run();
$this->assertFalse($this->jobList->has('OC\Cache\FileGlobalGC', null), 'Asserting that the job OC\Cache\FileGlobalGC does not exist after repairing');
$this->assertFalse($this->jobList->has('OC_Cache_FileGlobalGC', null), 'Asserting that the job OC_Cache_FileGlobalGC does not exist after repairing');
}
}