diff --git a/apps/user_ldap/lib/connection.php b/apps/user_ldap/lib/connection.php index c4e4efd048..7fbabda710 100644 --- a/apps/user_ldap/lib/connection.php +++ b/apps/user_ldap/lib/connection.php @@ -52,7 +52,7 @@ class Connection extends LDAPUtility { $this->configID = $configID; $this->configuration = new Configuration($configPrefix, !is_null($configID)); - $memcache = new \OC\Memcache\Factory(); + $memcache = \OC::$server->getMemCacheFactory(); if($memcache->isAvailable()) { $this->cache = $memcache->create(); } else { diff --git a/lib/private/memcache/apc.php b/lib/private/memcache/apc.php index e995cbc526..332bbfead0 100644 --- a/lib/private/memcache/apc.php +++ b/lib/private/memcache/apc.php @@ -9,15 +9,8 @@ namespace OC\Memcache; class APC extends Cache { - /** - * entries in APC gets namespaced to prevent collisions between owncloud instances and users - */ - protected function getNameSpace() { - return $this->prefix; - } - public function get($key) { - $result = apc_fetch($this->getNamespace() . $key, $success); + $result = apc_fetch($this->getPrefix() . $key, $success); if (!$success) { return null; } @@ -25,26 +18,22 @@ class APC extends Cache { } public function set($key, $value, $ttl = 0) { - return apc_store($this->getNamespace() . $key, $value, $ttl); + return apc_store($this->getPrefix() . $key, $value, $ttl); } public function hasKey($key) { - return apc_exists($this->getNamespace() . $key); + return apc_exists($this->getPrefix() . $key); } public function remove($key) { - return apc_delete($this->getNamespace() . $key); + return apc_delete($this->getPrefix() . $key); } public function clear($prefix = '') { - $ns = $this->getNamespace() . $prefix; - $cache = apc_cache_info('user'); - foreach ($cache['cache_list'] as $entry) { - if (strpos($entry['info'], $ns) === 0) { - apc_delete($entry['info']); - } - } - return true; + $ns = $this->getPrefix() . $prefix; + $ns = preg_quote($ns, '/'); + $iter = new \APCIterator('user', '/^' . $ns . '/'); + return apc_delete($iter); } static public function isAvailable() { diff --git a/lib/private/memcache/apcu.php b/lib/private/memcache/apcu.php index dac0f5f208..7f780f3271 100644 --- a/lib/private/memcache/apcu.php +++ b/lib/private/memcache/apcu.php @@ -9,13 +9,6 @@ namespace OC\Memcache; class APCu extends APC { - public function clear($prefix = '') { - $ns = $this->getNamespace() . $prefix; - $ns = preg_quote($ns, '/'); - $iter = new \APCIterator('user', '/^'.$ns.'/'); - return apc_delete($iter); - } - static public function isAvailable() { if (!extension_loaded('apcu')) { return false; diff --git a/lib/private/memcache/cache.php b/lib/private/memcache/cache.php index 0ad1cc7ec0..03671b3f24 100644 --- a/lib/private/memcache/cache.php +++ b/lib/private/memcache/cache.php @@ -18,7 +18,7 @@ abstract class Cache implements \ArrayAccess { * @param string $prefix */ public function __construct($prefix = '') { - $this->prefix = \OC_Util::getInstanceId() . '/' . $prefix; + $this->prefix = $prefix; } public function getPrefix() { diff --git a/lib/private/memcache/factory.php b/lib/private/memcache/factory.php index fde7d94756..334cf9a1f0 100644 --- a/lib/private/memcache/factory.php +++ b/lib/private/memcache/factory.php @@ -8,7 +8,21 @@ namespace OC\Memcache; -class Factory { +use \OCP\ICacheFactory; + +class Factory implements ICacheFactory { + /** + * @var string $globalPrefix + */ + private $globalPrefix; + + /** + * @param string $globalPrefix + */ + public function __construct($globalPrefix) { + $this->globalPrefix = $globalPrefix; + } + /** * get a cache instance, will return null if no backend is available * @@ -16,6 +30,7 @@ class Factory { * @return \OC\Memcache\Cache */ function create($prefix = '') { + $prefix = $this->globalPrefix . '/' . $prefix; if (XCache::isAvailable()) { return new XCache($prefix); } elseif (APCu::isAvailable()) { diff --git a/lib/private/server.php b/lib/private/server.php index 2cbd37a97d..c9e593ec2e 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -138,6 +138,10 @@ class Server extends SimpleContainer implements IServerContainer { $this->registerService('UserCache', function($c) { return new UserCache(); }); + $this->registerService('MemCacheFactory', function ($c) { + $instanceId = \OC_Util::getInstanceId(); + return new \OC\Memcache\Factory($instanceId); + }); $this->registerService('ActivityManager', function($c) { return new ActivityManager(); }); @@ -297,6 +301,15 @@ class Server extends SimpleContainer implements IServerContainer { return $this->query('UserCache'); } + /** + * Returns an \OCP\CacheFactory instance + * + * @return \OCP\CacheFactory + */ + function getMemCacheFactory() { + return $this->query('MemCacheFactory'); + } + /** * Returns the current session * diff --git a/lib/public/icachefactory.php b/lib/public/icachefactory.php new file mode 100644 index 0000000000..874f1ec0a5 --- /dev/null +++ b/lib/public/icachefactory.php @@ -0,0 +1,28 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP; + +interface ICacheFactory{ + /** + * Get a memory cache instance + * + * All entries added trough the cache instance will be namespaced by $prefix to prevent collisions between apps + * + * @param string $prefix + * @return \OCP\ICache + */ + public function create($prefix = ''); + + /** + * Check if any memory cache backend is available + * + * @return bool + */ + public function isAvailable(); +} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index b958d2d03f..5473f3ee33 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -141,6 +141,13 @@ interface IServerContainer { */ function getCache(); + /** + * Returns an \OCP\CacheFactory instance + * + * @return \OCP\ICacheFactory + */ + function getMemCacheFactory(); + /** * Returns the current session *