Merge pull request #6647 from owncloud/memcache-public

Add Memcache to the public api
This commit is contained in:
icewind1991 2014-02-04 05:38:53 -08:00
commit a27529709b
8 changed files with 74 additions and 29 deletions

View File

@ -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 {

View File

@ -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() {

View File

@ -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;

View File

@ -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() {

View File

@ -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()) {

View File

@ -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
*

View File

@ -0,0 +1,28 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* 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();
}

View File

@ -141,6 +141,13 @@ interface IServerContainer {
*/
function getCache();
/**
* Returns an \OCP\CacheFactory instance
*
* @return \OCP\ICacheFactory
*/
function getMemCacheFactory();
/**
* Returns the current session
*