From fe86182dac387817258942a46905f2b801862d4d Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Tue, 17 Sep 2013 17:46:33 +0200 Subject: [PATCH 1/6] OC_Cache namespace changes and add UserCache to server container. Refs #4863 --- lib/base.php | 4 +- lib/cache.php | 8 ++-- lib/cache/broker.php | 4 +- lib/cache/file.php | 13 +++--- lib/cache/fileglobal.php | 7 +-- lib/cache/fileglobalgc.php | 5 ++- lib/cache/usercache.php | 77 +++++++++++++++++++++++++++++++++ lib/filechunking.php | 2 +- lib/public/icache.php | 55 +++++++++++++++++++++++ lib/public/iservercontainer.php | 7 +++ lib/server.php | 34 ++++++++++----- tests/lib/cache/file.php | 30 +++++++------ tests/lib/cache/usercache.php | 68 +++++++++++++++++++++++++++++ 13 files changed, 270 insertions(+), 44 deletions(-) create mode 100644 lib/cache/usercache.php create mode 100644 lib/public/icache.php create mode 100644 tests/lib/cache/usercache.php diff --git a/lib/base.php b/lib/base.php index 1720a5fd7e..520be11bc5 100644 --- a/lib/base.php +++ b/lib/base.php @@ -564,11 +564,11 @@ class OC { if (OC_Config::getValue('installed', false)) { //don't try to do this before we are properly setup // register cache cleanup jobs 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) { } - OC_Hook::connect('OC_User', 'post_login', 'OC_Cache_File', 'loginListener'); + OC_Hook::connect('OC_User', 'post_login', 'OC\Cache\File', 'loginListener'); } } diff --git a/lib/cache.php b/lib/cache.php index 48b9964ba9..c99663a0ca 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -6,7 +6,9 @@ * See the COPYING-README file. */ -class OC_Cache { +namespace OC\Cache; + +class Cache { /** * @var OC_Cache $user_cache */ @@ -22,7 +24,7 @@ class OC_Cache { */ static public function getGlobalCache() { if (!self::$global_cache) { - self::$global_cache = new OC_Cache_FileGlobal(); + self::$global_cache = new FileGlobal(); } return self::$global_cache; } @@ -33,7 +35,7 @@ class OC_Cache { */ static public function getUserCache() { if (!self::$user_cache) { - self::$user_cache = new OC_Cache_File(); + self::$user_cache = new File(); } return self::$user_cache; } diff --git a/lib/cache/broker.php b/lib/cache/broker.php index a161dbfa3b..b7f1b67a6d 100644 --- a/lib/cache/broker.php +++ b/lib/cache/broker.php @@ -6,7 +6,9 @@ * See the COPYING-README file. */ -class OC_Cache_Broker { +namespace OC\Cache; + +class Broker { protected $fast_cache; protected $slow_cache; diff --git a/lib/cache/file.php b/lib/cache/file.php index 361138e473..2ab914d17b 100644 --- a/lib/cache/file.php +++ b/lib/cache/file.php @@ -6,24 +6,25 @@ * See the COPYING-README file. */ +namespace OC\Cache; -class OC_Cache_File{ +class File { protected $storage; protected function getStorage() { if (isset($this->storage)) { return $this->storage; } - if(OC_User::isLoggedIn()) { - \OC\Files\Filesystem::initMountPoints(OC_User::getUser()); + if(\OC_User::isLoggedIn()) { + \OC\Files\Filesystem::initMountPoints(\OC_User::getUser()); $subdir = 'cache'; - $view = new \OC\Files\View('/'.OC_User::getUser()); + $view = new \OC\Files\View('/' . \OC_User::getUser()); if(!$view->file_exists($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; }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; } } diff --git a/lib/cache/fileglobal.php b/lib/cache/fileglobal.php index c0bd8e45f3..9ca1740293 100644 --- a/lib/cache/fileglobal.php +++ b/lib/cache/fileglobal.php @@ -6,8 +6,9 @@ * See the COPYING-README file. */ +namespace OC\Cache; -class OC_Cache_FileGlobal{ +class FileGlobal { static protected function getCacheDir() { $cache_dir = get_temp_dir().'/owncloud-'.OC_Util::getInstanceId().'/'; if (!is_dir($cache_dir)) { @@ -80,13 +81,13 @@ class OC_Cache_FileGlobal{ } 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(); if (($now - $last_run) < 300) { // only do cleanup every 5 minutes return; } - OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now); + \OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now); $cache_dir = self::getCacheDir(); if($cache_dir and is_dir($cache_dir)) { $dh=opendir($cache_dir); diff --git a/lib/cache/fileglobalgc.php b/lib/cache/fileglobalgc.php index a29c31f906..399dd5e6f9 100644 --- a/lib/cache/fileglobalgc.php +++ b/lib/cache/fileglobalgc.php @@ -1,8 +1,9 @@ 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); + } +} diff --git a/lib/filechunking.php b/lib/filechunking.php index e6d69273a4..313a6ee87d 100644 --- a/lib/filechunking.php +++ b/lib/filechunking.php @@ -29,7 +29,7 @@ class OC_FileChunking { protected function getCache() { if (!isset($this->cache)) { - $this->cache = new OC_Cache_File(); + $this->cache = new \OC\Cache\File(); } return $this->cache; } diff --git a/lib/public/icache.php b/lib/public/icache.php new file mode 100644 index 0000000000..202459f7c2 --- /dev/null +++ b/lib/public/icache.php @@ -0,0 +1,55 @@ +registerService('ContactsManager', function($c){ + $this->registerService('ContactsManager', function($c) { return new ContactsManager(); }); - $this->registerService('Request', function($c){ + $this->registerService('Request', function($c) { $params = array(); // we json decode the body only in case of content type json @@ -46,10 +46,10 @@ class Server extends SimpleContainer implements IServerContainer { ) ); }); - $this->registerService('PreviewManager', function($c){ + $this->registerService('PreviewManager', function($c) { return new PreviewManager(); }); - $this->registerService('RootFolder', function($c){ + $this->registerService('RootFolder', function($c) { // TODO: get user and user manager from container as well $user = \OC_User::getUser(); $user = \OC_User::getManager()->get($user); @@ -57,6 +57,9 @@ class Server extends SimpleContainer implements IServerContainer { $view = new View(); return new Root($manager, $view, $user); }); + $this->registerService('UserCache', function($c) { + return new UserCache(); + }); } /** @@ -67,14 +70,13 @@ class Server extends SimpleContainer implements IServerContainer { } /** - * The current request object holding all information about the request currently being processed - * is returned from this method. + * The current request object holding all information about the request + * currently being processed is returned from this method. * In case the current execution was not initiated by a web request null is returned * * @return \OCP\IRequest|null */ - function getRequest() - { + function getRequest() { return $this->query('Request'); } @@ -83,8 +85,7 @@ class Server extends SimpleContainer implements IServerContainer { * * @return \OCP\IPreview */ - function getPreviewManager() - { + function getPreviewManager() { return $this->query('PreviewManager'); } @@ -93,8 +94,17 @@ class Server extends SimpleContainer implements IServerContainer { * * @return \OCP\Files\Folder */ - function getRootFolder() - { + function getRootFolder() { return $this->query('RootFolder'); } + + /** + * Returns an ICache instance + * + * @return \OCP\ICache + */ + function getCache() { + return $this->query('UserCache'); + } + } diff --git a/tests/lib/cache/file.php b/tests/lib/cache/file.php index 038cb21b25..3767c83fcb 100644 --- a/tests/lib/cache/file.php +++ b/tests/lib/cache/file.php @@ -20,7 +20,9 @@ * */ -class Test_Cache_File extends Test_Cache { +namespace Test\Cache; + +class FileCache extends \Test_Cache { private $user; private $datadir; @@ -30,8 +32,8 @@ class Test_Cache_File extends Test_Cache { public function setUp() { //clear all proxies and hooks so we can do clean testing - OC_FileProxy::clearProxies(); - OC_Hook::clear('OC_Filesystem'); + \OC_FileProxy::clearProxies(); + \OC_Hook::clear('OC_Filesystem'); //disabled atm //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()); \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); + $this->datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT.'/data'); + \OC_Config::setValue('datadirectory', $datadir); - OC_User::clearBackends(); - OC_User::useBackend(new OC_User_Dummy()); + \OC_User::clearBackends(); + \OC_User::useBackend(new \OC_User_Dummy()); //login - OC_User::createUser('test', 'test'); + \OC_User::createUser('test', 'test'); - $this->user=OC_User::getUser(); - OC_User::setUserId('test'); + $this->user = \OC_User::getUser(); + \OC_User::setUserId('test'); //set up the users dir - $rootView=new \OC\Files\View(''); + $rootView = new \OC\Files\View(''); $rootView->mkdir('/test'); - $this->instance=new OC_Cache_File(); + $this->instance=new \OC\Cache\File(); } public function tearDown() { - OC_User::setUserId($this->user); - OC_Config::setValue('datadirectory', $this->datadir); + \OC_User::setUserId($this->user); + \OC_Config::setValue('datadirectory', $this->datadir); } } diff --git a/tests/lib/cache/usercache.php b/tests/lib/cache/usercache.php new file mode 100644 index 0000000000..21b7f848ab --- /dev/null +++ b/tests/lib/cache/usercache.php @@ -0,0 +1,68 @@ +. +* +*/ + +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); + } +} From 1a130627012bb17ed9edc4583a4d8250ff4e2882 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Tue, 17 Sep 2013 18:02:37 +0200 Subject: [PATCH 2/6] Add legacy wrapper --- lib/legacy/cache.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/legacy/cache.php diff --git a/lib/legacy/cache.php b/lib/legacy/cache.php new file mode 100644 index 0000000000..83b214170f --- /dev/null +++ b/lib/legacy/cache.php @@ -0,0 +1,10 @@ + Date: Wed, 18 Sep 2013 13:15:38 +0200 Subject: [PATCH 3/6] Use new emitter system --- lib/base.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/base.php b/lib/base.php index 520be11bc5..ce307b2bdd 100644 --- a/lib/base.php +++ b/lib/base.php @@ -568,7 +568,9 @@ class OC { } 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', array('OC\Cache\File', 'loginListener')) } } From 79cd655920ae3346725539df2f443a66e51c5726 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Wed, 18 Sep 2013 14:50:21 +0200 Subject: [PATCH 4/6] Note to self: Test before pushing!!! --- lib/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/base.php b/lib/base.php index ce307b2bdd..fd31cfd9f2 100644 --- a/lib/base.php +++ b/lib/base.php @@ -570,7 +570,7 @@ class OC { } // NOTE: This will be replaced to use OCP $userSession = \OC_User::getUserSession(); - $userSession->listen('postLogin', array('OC\Cache\File', 'loginListener')) + $userSession->listen('postLogin', array('OC\Cache\File', 'loginListener')); } } From 09d043729a41a0e8966ed3bb81567ed1009a37b6 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Wed, 18 Sep 2013 15:02:25 +0200 Subject: [PATCH 5/6] Note to self 2: Do as you preach. Test! --- lib/base.php | 2 +- lib/cache.php | 6 +++--- lib/cache/fileglobal.php | 2 +- lib/legacy/cache.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/base.php b/lib/base.php index fd31cfd9f2..0650361be9 100644 --- a/lib/base.php +++ b/lib/base.php @@ -570,7 +570,7 @@ class OC { } // NOTE: This will be replaced to use OCP $userSession = \OC_User::getUserSession(); - $userSession->listen('postLogin', array('OC\Cache\File', 'loginListener')); + $userSession->listen('postLogin', '\OC\Cache\File', 'loginListener'); } } diff --git a/lib/cache.php b/lib/cache.php index c99663a0ca..a4fa8be710 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -6,7 +6,7 @@ * See the COPYING-README file. */ -namespace OC\Cache; +namespace OC; class Cache { /** @@ -24,7 +24,7 @@ class Cache { */ static public function getGlobalCache() { if (!self::$global_cache) { - self::$global_cache = new FileGlobal(); + self::$global_cache = new Cache\FileGlobal(); } return self::$global_cache; } @@ -35,7 +35,7 @@ class Cache { */ static public function getUserCache() { if (!self::$user_cache) { - self::$user_cache = new File(); + self::$user_cache = new Cache\File(); } return self::$user_cache; } diff --git a/lib/cache/fileglobal.php b/lib/cache/fileglobal.php index 9ca1740293..bd049bba4d 100644 --- a/lib/cache/fileglobal.php +++ b/lib/cache/fileglobal.php @@ -10,7 +10,7 @@ namespace OC\Cache; class FileGlobal { 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)) { mkdir($cache_dir); } diff --git a/lib/legacy/cache.php b/lib/legacy/cache.php index 83b214170f..f915eb516b 100644 --- a/lib/legacy/cache.php +++ b/lib/legacy/cache.php @@ -6,5 +6,5 @@ * See the COPYING-README file. */ -class Cache extends OC\Cache { +class OC_Cache extends \OC\Cache { } \ No newline at end of file From 1274d6116dc59a8238ad9d02d467260387fe2eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 18 Sep 2013 22:22:51 +0200 Subject: [PATCH 6/6] updating php docs --- lib/cache.php | 15 ++++++++++----- lib/cache/broker.php | 8 ++++++++ lib/cache/usercache.php | 4 ++-- lib/public/icache.php | 2 +- tests/lib/cache.php | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/cache.php b/lib/cache.php index a4fa8be710..a311f10a00 100644 --- a/lib/cache.php +++ b/lib/cache.php @@ -10,17 +10,17 @@ namespace OC; class Cache { /** - * @var OC_Cache $user_cache + * @var Cache $user_cache */ static protected $user_cache; /** - * @var OC_Cache $global_cache + * @var Cache $global_cache */ static protected $global_cache; /** * get the global cache - * @return OC_Cache + * @return Cache */ static public function getGlobalCache() { if (!self::$global_cache) { @@ -31,7 +31,7 @@ class Cache { /** * get the user cache - * @return OC_Cache + * @return Cache */ static public function getUserCache() { if (!self::$user_cache) { @@ -87,7 +87,7 @@ class Cache { /** * clear the user cache of all entries starting with a prefix - * @param string prefix (optional) + * @param string $prefix (optional) * @return bool */ static public function clear($prefix='') { @@ -95,6 +95,11 @@ class Cache { return $user_cache->clear($prefix); } + /** + * creates cache key based on the files given + * @param $files + * @return string + */ static public function generateCacheKeyFromFiles($files) { $key = ''; sort($files); diff --git a/lib/cache/broker.php b/lib/cache/broker.php index b7f1b67a6d..9b7e837e1b 100644 --- a/lib/cache/broker.php +++ b/lib/cache/broker.php @@ -9,7 +9,15 @@ namespace OC\Cache; class Broker { + + /** + * @var \OC\Cache + */ protected $fast_cache; + + /** + * @var \OC\Cache + */ protected $slow_cache; public function __construct($fast_cache, $slow_cache) { diff --git a/lib/cache/usercache.php b/lib/cache/usercache.php index aac3b39af3..baa8820700 100644 --- a/lib/cache/usercache.php +++ b/lib/cache/usercache.php @@ -13,7 +13,7 @@ namespace OC\Cache; class UserCache implements \OCP\ICache { /** - * @var OC\Cache\File $userCache + * @var \OC\Cache\File $userCache */ protected $userCache; @@ -68,7 +68,7 @@ class UserCache implements \OCP\ICache { /** * clear the user cache of all entries starting with a prefix - * @param string prefix (optional) + * @param string $prefix (optional) * @return bool */ public function clear($prefix = '') { diff --git a/lib/public/icache.php b/lib/public/icache.php index 202459f7c2..436ee71b2b 100644 --- a/lib/public/icache.php +++ b/lib/public/icache.php @@ -48,7 +48,7 @@ interface ICache { /** * clear the user cache of all entries starting with a prefix - * @param string prefix (optional) + * @param string $prefix (optional) * @return bool */ public function clear($prefix = ''); diff --git a/tests/lib/cache.php b/tests/lib/cache.php index 3dcf39f7d6..8fefa25f65 100644 --- a/tests/lib/cache.php +++ b/tests/lib/cache.php @@ -8,7 +8,7 @@ abstract class Test_Cache extends PHPUnit_Framework_TestCase { /** - * @var OC_Cache cache; + * @var \OC\Cache cache; */ protected $instance;