Merge pull request #4886 from owncloud/public_cache

Public cache etc.
This commit is contained in:
Thomas Müller 2013-09-18 13:29:35 -07:00
commit 69c28400b4
15 changed files with 301 additions and 51 deletions

View File

@ -564,11 +564,13 @@ class OC {
if (OC_Config::getValue('installed', false)) { //don't try to do this before we are properly setup if (OC_Config::getValue('installed', false)) { //don't try to do this before we are properly setup
// register cache cleanup jobs // register cache cleanup jobs
try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception
\OCP\BackgroundJob::registerJob('OC_Cache_FileGlobalGC'); \OCP\BackgroundJob::registerJob('OC\Cache\FileGlobalGC');
} catch (Exception $e) { } catch (Exception $e) {
} }
OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); // NOTE: This will be replaced to use OCP
$userSession = \OC_User::getUserSession();
$userSession->listen('postLogin', '\OC\Cache\File', 'loginListener');
} }
} }

View File

@ -6,34 +6,36 @@
* See the COPYING-README file. * See the COPYING-README file.
*/ */
class OC_Cache { namespace OC;
class Cache {
/** /**
* @var OC_Cache $user_cache * @var Cache $user_cache
*/ */
static protected $user_cache; static protected $user_cache;
/** /**
* @var OC_Cache $global_cache * @var Cache $global_cache
*/ */
static protected $global_cache; static protected $global_cache;
/** /**
* get the global cache * get the global cache
* @return OC_Cache * @return Cache
*/ */
static public function getGlobalCache() { static public function getGlobalCache() {
if (!self::$global_cache) { if (!self::$global_cache) {
self::$global_cache = new OC_Cache_FileGlobal(); self::$global_cache = new Cache\FileGlobal();
} }
return self::$global_cache; return self::$global_cache;
} }
/** /**
* get the user cache * get the user cache
* @return OC_Cache * @return Cache
*/ */
static public function getUserCache() { static public function getUserCache() {
if (!self::$user_cache) { if (!self::$user_cache) {
self::$user_cache = new OC_Cache_File(); self::$user_cache = new Cache\File();
} }
return self::$user_cache; return self::$user_cache;
} }
@ -85,7 +87,7 @@ class OC_Cache {
/** /**
* clear the user cache of all entries starting with a prefix * clear the user cache of all entries starting with a prefix
* @param string prefix (optional) * @param string $prefix (optional)
* @return bool * @return bool
*/ */
static public function clear($prefix='') { static public function clear($prefix='') {
@ -93,6 +95,11 @@ class OC_Cache {
return $user_cache->clear($prefix); return $user_cache->clear($prefix);
} }
/**
* creates cache key based on the files given
* @param $files
* @return string
*/
static public function generateCacheKeyFromFiles($files) { static public function generateCacheKeyFromFiles($files) {
$key = ''; $key = '';
sort($files); sort($files);

12
lib/cache/broker.php vendored
View File

@ -6,8 +6,18 @@
* See the COPYING-README file. * See the COPYING-README file.
*/ */
class OC_Cache_Broker { namespace OC\Cache;
class Broker {
/**
* @var \OC\Cache
*/
protected $fast_cache; protected $fast_cache;
/**
* @var \OC\Cache
*/
protected $slow_cache; protected $slow_cache;
public function __construct($fast_cache, $slow_cache) { public function __construct($fast_cache, $slow_cache) {

13
lib/cache/file.php vendored
View File

@ -6,24 +6,25 @@
* See the COPYING-README file. * See the COPYING-README file.
*/ */
namespace OC\Cache;
class OC_Cache_File{ class File {
protected $storage; protected $storage;
protected function getStorage() { protected function getStorage() {
if (isset($this->storage)) { if (isset($this->storage)) {
return $this->storage; return $this->storage;
} }
if(OC_User::isLoggedIn()) { if(\OC_User::isLoggedIn()) {
\OC\Files\Filesystem::initMountPoints(OC_User::getUser()); \OC\Files\Filesystem::initMountPoints(\OC_User::getUser());
$subdir = 'cache'; $subdir = 'cache';
$view = new \OC\Files\View('/'.OC_User::getUser()); $view = new \OC\Files\View('/' . \OC_User::getUser());
if(!$view->file_exists($subdir)) { if(!$view->file_exists($subdir)) {
$view->mkdir($subdir); $view->mkdir($subdir);
} }
$this->storage = new \OC\Files\View('/'.OC_User::getUser().'/'.$subdir); $this->storage = new \OC\Files\View('/' . \OC_User::getUser().'/'.$subdir);
return $this->storage; return $this->storage;
}else{ }else{
OC_Log::write('core', 'Can\'t get cache storage, user not logged in', OC_Log::ERROR); \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
return false; return false;
} }
} }

View File

@ -6,10 +6,11 @@
* See the COPYING-README file. * See the COPYING-README file.
*/ */
namespace OC\Cache;
class OC_Cache_FileGlobal{ class FileGlobal {
static protected function getCacheDir() { static protected function getCacheDir() {
$cache_dir = get_temp_dir().'/owncloud-'.OC_Util::getInstanceId().'/'; $cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
if (!is_dir($cache_dir)) { if (!is_dir($cache_dir)) {
mkdir($cache_dir); mkdir($cache_dir);
} }
@ -80,13 +81,13 @@ class OC_Cache_FileGlobal{
} }
static public function gc() { static public function gc() {
$last_run = OC_AppConfig::getValue('core', 'global_cache_gc_lastrun', 0); $last_run = \OC_AppConfig::getValue('core', 'global_cache_gc_lastrun', 0);
$now = time(); $now = time();
if (($now - $last_run) < 300) { if (($now - $last_run) < 300) {
// only do cleanup every 5 minutes // only do cleanup every 5 minutes
return; return;
} }
OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now); \OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now);
$cache_dir = self::getCacheDir(); $cache_dir = self::getCacheDir();
if($cache_dir and is_dir($cache_dir)) { if($cache_dir and is_dir($cache_dir)) {
$dh=opendir($cache_dir); $dh=opendir($cache_dir);

View File

@ -1,8 +1,9 @@
<?php <?php
namespace OC\Cache;
class OC_Cache_FileGlobalGC extends \OC\BackgroundJob\Job{ class FileGlobalGC extends \OC\BackgroundJob\Job{
public function run($argument){ public function run($argument){
OC_Cache_FileGlobal::gc(); FileGlobal::gc();
} }
} }

77
lib/cache/usercache.php vendored Normal file
View File

@ -0,0 +1,77 @@
<?php
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
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 mixed $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

@ -29,7 +29,7 @@ class OC_FileChunking {
protected function getCache() { protected function getCache() {
if (!isset($this->cache)) { if (!isset($this->cache)) {
$this->cache = new OC_Cache_File(); $this->cache = new \OC\Cache\File();
} }
return $this->cache; return $this->cache;
} }

10
lib/legacy/cache.php Normal file
View File

@ -0,0 +1,10 @@
<?php
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_Cache extends \OC\Cache {
}

55
lib/public/icache.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCP;
/**
* This interface defines method for accessing the file based user cache.
*/
interface ICache {
/**
* Get a value from the user cache
*
* @param string $key
* @return mixed
*/
public function get($key);
/**
* Set a value in the user cache
*
* @param string $key
* @param mixed $value
* @param int $ttl Time To Live in seconds. Defaults to 60*60*24
* @return bool
*/
public function set($key, $value, $ttl = 0);
/**
* Check if a value is set in the user cache
*
* @param string $key
* @return bool
*/
public function hasKey($key);
/**
* Remove an item from the user cache
*
* @param string $key
* @return bool
*/
public function remove($key);
/**
* clear the user cache of all entries starting with a prefix
* @param string $prefix (optional)
* @return bool
*/
public function clear($prefix = '');
}

View File

@ -62,6 +62,13 @@ interface IServerContainer {
*/ */
function getRootFolder(); function getRootFolder();
/**
* Returns an ICache instance
*
* @return \OCP\ICache
*/
function getCache();
/** /**
* Returns the current session * Returns the current session
* *

View File

@ -17,10 +17,10 @@ use OCP\IServerContainer;
class Server extends SimpleContainer implements IServerContainer { class Server extends SimpleContainer implements IServerContainer {
function __construct() { function __construct() {
$this->registerService('ContactsManager', function($c){ $this->registerService('ContactsManager', function($c) {
return new ContactsManager(); return new ContactsManager();
}); });
$this->registerService('Request', function($c){ $this->registerService('Request', function($c) {
$params = array(); $params = array();
// we json decode the body only in case of content type json // we json decode the body only in case of content type json
@ -45,10 +45,10 @@ class Server extends SimpleContainer implements IServerContainer {
) )
); );
}); });
$this->registerService('PreviewManager', function($c){ $this->registerService('PreviewManager', function($c) {
return new PreviewManager(); return new PreviewManager();
}); });
$this->registerService('RootFolder', function($c){ $this->registerService('RootFolder', function($c) {
// TODO: get user and user manager from container as well // TODO: get user and user manager from container as well
$user = \OC_User::getUser(); $user = \OC_User::getUser();
$user = \OC_User::getManager()->get($user); $user = \OC_User::getManager()->get($user);
@ -56,6 +56,9 @@ class Server extends SimpleContainer implements IServerContainer {
$view = new View(); $view = new View();
return new Root($manager, $view, $user); return new Root($manager, $view, $user);
}); });
$this->registerService('UserCache', function($c) {
return new UserCache();
});
} }
/** /**
@ -66,14 +69,13 @@ class Server extends SimpleContainer implements IServerContainer {
} }
/** /**
* The current request object holding all information about the request currently being processed * The current request object holding all information about the request
* is returned from this method. * currently being processed is returned from this method.
* In case the current execution was not initiated by a web request null is returned * In case the current execution was not initiated by a web request null is returned
* *
* @return \OCP\IRequest|null * @return \OCP\IRequest|null
*/ */
function getRequest() function getRequest() {
{
return $this->query('Request'); return $this->query('Request');
} }
@ -82,8 +84,7 @@ class Server extends SimpleContainer implements IServerContainer {
* *
* @return \OCP\IPreview * @return \OCP\IPreview
*/ */
function getPreviewManager() function getPreviewManager() {
{
return $this->query('PreviewManager'); return $this->query('PreviewManager');
} }
@ -92,11 +93,19 @@ class Server extends SimpleContainer implements IServerContainer {
* *
* @return \OCP\Files\Folder * @return \OCP\Files\Folder
*/ */
function getRootFolder() function getRootFolder() {
{
return $this->query('RootFolder'); return $this->query('RootFolder');
} }
/**
* Returns an ICache instance
*
* @return \OCP\ICache
*/
function getCache() {
return $this->query('UserCache');
}
/** /**
* Returns the current session * Returns the current session
* *

View File

@ -8,7 +8,7 @@
abstract class Test_Cache extends PHPUnit_Framework_TestCase { abstract class Test_Cache extends PHPUnit_Framework_TestCase {
/** /**
* @var OC_Cache cache; * @var \OC\Cache cache;
*/ */
protected $instance; protected $instance;

View File

@ -20,7 +20,9 @@
* *
*/ */
class Test_Cache_File extends Test_Cache { namespace Test\Cache;
class FileCache extends \Test_Cache {
private $user; private $user;
private $datadir; private $datadir;
@ -30,8 +32,8 @@ class Test_Cache_File extends Test_Cache {
public function setUp() { public function setUp() {
//clear all proxies and hooks so we can do clean testing //clear all proxies and hooks so we can do clean testing
OC_FileProxy::clearProxies(); \OC_FileProxy::clearProxies();
OC_Hook::clear('OC_Filesystem'); \OC_Hook::clear('OC_Filesystem');
//disabled atm //disabled atm
//enable only the encryption hook if needed //enable only the encryption hook if needed
@ -44,27 +46,27 @@ class Test_Cache_File extends Test_Cache {
$storage = new \OC\Files\Storage\Temporary(array()); $storage = new \OC\Files\Storage\Temporary(array());
\OC\Files\Filesystem::mount($storage,array(),'/'); \OC\Files\Filesystem::mount($storage,array(),'/');
$datadir = str_replace('local::', '', $storage->getId()); $datadir = str_replace('local::', '', $storage->getId());
$this->datadir = OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data'); $this->datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT.'/data');
OC_Config::setValue('datadirectory', $datadir); \OC_Config::setValue('datadirectory', $datadir);
OC_User::clearBackends(); \OC_User::clearBackends();
OC_User::useBackend(new OC_User_Dummy()); \OC_User::useBackend(new \OC_User_Dummy());
//login //login
OC_User::createUser('test', 'test'); \OC_User::createUser('test', 'test');
$this->user=OC_User::getUser(); $this->user = \OC_User::getUser();
OC_User::setUserId('test'); \OC_User::setUserId('test');
//set up the users dir //set up the users dir
$rootView=new \OC\Files\View(''); $rootView = new \OC\Files\View('');
$rootView->mkdir('/test'); $rootView->mkdir('/test');
$this->instance=new OC_Cache_File(); $this->instance=new \OC\Cache\File();
} }
public function tearDown() { public function tearDown() {
OC_User::setUserId($this->user); \OC_User::setUserId($this->user);
OC_Config::setValue('datadirectory', $this->datadir); \OC_Config::setValue('datadirectory', $this->datadir);
} }
} }

68
tests/lib/cache/usercache.php vendored Normal file
View File

@ -0,0 +1,68 @@
<?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 {
private $user;
private $datadir;
public function setUp() {
//clear all proxies and hooks so we can do clean testing
\OC_FileProxy::clearProxies();
\OC_Hook::clear('OC_Filesystem');
//disabled atm
//enable only the encryption hook if needed
//if(OC_App::isEnabled('files_encryption')) {
// OC_FileProxy::register(new OC_FileProxy_Encryption());
//}
//set up temporary storage
\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('datadirectory', \OC::$SERVERROOT.'/data');
\OC_Config::setValue('datadirectory', $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();
}
public function tearDown() {
\OC_User::setUserId($this->user);
\OC_Config::setValue('datadirectory', $this->datadir);
}
}