Merge pull request #2395 from owncloud/cache
Seperate the memory based cache and file based cache in OC_Cacheremotes/origin/stable6
commit
e09ffb6f57
@ -1,64 +0,0 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
class OC_Cache_APC { |
||||
protected $prefix; |
||||
|
||||
public function __construct($global = false) { |
||||
$this->prefix = OC_Util::getInstanceId().'/'; |
||||
if (!$global) { |
||||
$this->prefix .= OC_User::getUser().'/'; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 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); |
||||
if (!$success) { |
||||
return null; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public function set($key, $value, $ttl=0) { |
||||
return apc_store($this->getNamespace().$key, $value, $ttl); |
||||
} |
||||
|
||||
public function hasKey($key) { |
||||
return apc_exists($this->getNamespace().$key); |
||||
} |
||||
|
||||
public function remove($key) { |
||||
return apc_delete($this->getNamespace().$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; |
||||
} |
||||
} |
||||
if(!function_exists('apc_exists')) { |
||||
function apc_exists($keys) |
||||
{ |
||||
$result=false; |
||||
apc_fetch($keys, $result); |
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,67 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
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); |
||||
if (!$success) { |
||||
return null; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public function set($key, $value, $ttl = 0) { |
||||
return apc_store($this->getNamespace() . $key, $value, $ttl); |
||||
} |
||||
|
||||
public function hasKey($key) { |
||||
return apc_exists($this->getNamespace() . $key); |
||||
} |
||||
|
||||
public function remove($key) { |
||||
return apc_delete($this->getNamespace() . $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; |
||||
} |
||||
|
||||
static public function isAvailable() { |
||||
if (!extension_loaded('apc')) { |
||||
return false; |
||||
} elseif (!ini_get('apc.enable_cli') && \OC::$CLI) { |
||||
return false; |
||||
} else { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (!function_exists('apc_exists')) { |
||||
function apc_exists($keys) { |
||||
$result = false; |
||||
apc_fetch($keys, $result); |
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,77 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2013 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 OC\Memcache; |
||||
|
||||
abstract class Cache implements \ArrayAccess { |
||||
/** |
||||
* @var string $prefix |
||||
*/ |
||||
protected $prefix; |
||||
|
||||
/** |
||||
* @param string $prefix |
||||
*/ |
||||
public function __construct($prefix = '') { |
||||
$this->prefix = \OC_Util::getInstanceId() . '/' . $prefix; |
||||
} |
||||
|
||||
public function getPrefix() { |
||||
return $this->prefix; |
||||
} |
||||
|
||||
/** |
||||
* @param string $key |
||||
* @return mixed |
||||
*/ |
||||
abstract public function get($key); |
||||
|
||||
/** |
||||
* @param string $key |
||||
* @param mixed $value |
||||
* @param int $ttl |
||||
* @return mixed |
||||
*/ |
||||
abstract public function set($key, $value, $ttl = 0); |
||||
|
||||
/** |
||||
* @param string $key |
||||
* @return mixed |
||||
*/ |
||||
abstract public function hasKey($key); |
||||
|
||||
/** |
||||
* @param string $key |
||||
* @return mixed |
||||
*/ |
||||
abstract public function remove($key); |
||||
|
||||
/** |
||||
* @param string $prefix |
||||
* @return mixed |
||||
*/ |
||||
abstract public function clear($prefix = ''); |
||||
|
||||
//implement the ArrayAccess interface |
||||
|
||||
public function offsetExists($offset) { |
||||
return $this->hasKey($offset); |
||||
} |
||||
|
||||
public function offsetSet($offset, $value) { |
||||
$this->set($offset, $value); |
||||
} |
||||
|
||||
public function offsetGet($offset) { |
||||
return $this->get($offset); |
||||
} |
||||
|
||||
public function offsetUnset($offset) { |
||||
$this->remove($offset); |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2013 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 OC\Memcache; |
||||
|
||||
class Factory { |
||||
/** |
||||
* get a cache instance, will return null if no backend is available |
||||
* |
||||
* @param string $prefix |
||||
* @return \OC\Memcache\Cache |
||||
*/ |
||||
function create($prefix = '') { |
||||
if (XCache::isAvailable()) { |
||||
return new XCache($prefix); |
||||
} elseif (APC::isAvailable()) { |
||||
return new APC($prefix); |
||||
} elseif (Memcached::isAvailable()) { |
||||
return new Memcached($prefix); |
||||
} else { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* check if there is a memcache backend available |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function isAvailable() { |
||||
return XCache::isAvailable() || APC::isAvailable() || Memcached::isAvailable(); |
||||
} |
||||
} |
@ -0,0 +1,76 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OC\Memcache; |
||||
|
||||
class Memcached extends Cache { |
||||
/** |
||||
* @var \Memcached $cache |
||||
*/ |
||||
private static $cache = null; |
||||
|
||||
public function __construct($prefix = '') { |
||||
parent::__construct($prefix); |
||||
if (is_null(self::$cache)) { |
||||
self::$cache = new \Memcached(); |
||||
list($host, $port) = \OC_Config::getValue('memcached_server', array('localhost', 11211)); |
||||
self::$cache->addServer($host, $port); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
||||
*/ |
||||
protected function getNameSpace() { |
||||
return $this->prefix; |
||||
} |
||||
|
||||
public function get($key) { |
||||
$result = self::$cache->get($this->getNamespace() . $key); |
||||
if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
||||
return null; |
||||
} else { |
||||
return $result; |
||||
} |
||||
} |
||||
|
||||
public function set($key, $value, $ttl = 0) { |
||||
if ($ttl > 0) { |
||||
return self::$cache->set($this->getNamespace() . $key, $value, $ttl); |
||||
} else { |
||||
return self::$cache->set($this->getNamespace() . $key, $value); |
||||
} |
||||
} |
||||
|
||||
public function hasKey($key) { |
||||
self::$cache->get($this->getNamespace() . $key); |
||||
return self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND; |
||||
} |
||||
|
||||
public function remove($key) { |
||||
return self::$cache->delete($this->getNamespace() . $key); |
||||
} |
||||
|
||||
public function clear($prefix = '') { |
||||
$prefix = $this->getNamespace() . $prefix; |
||||
$allKeys = self::$cache->getAllKeys(); |
||||
$keys = array(); |
||||
$prefixLength = strlen($prefix); |
||||
foreach ($allKeys as $key) { |
||||
if (substr($key, 0, $prefixLength) === $prefix) { |
||||
$keys[] = $key; |
||||
} |
||||
} |
||||
self::$cache->deleteMulti($keys); |
||||
return true; |
||||
} |
||||
|
||||
static public function isAvailable() { |
||||
return extension_loaded('memcached'); |
||||
} |
||||
} |
@ -1,35 +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/>. |
||||
* |
||||
*/ |
||||
|
||||
class Test_Cache_APC extends Test_Cache { |
||||
public function setUp() { |
||||
if(!extension_loaded('apc')) { |
||||
$this->markTestSkipped('The apc extension is not available.'); |
||||
return; |
||||
} |
||||
if(!ini_get('apc.enable_cli') && OC::$CLI) { |
||||
$this->markTestSkipped('apc not available in CLI.'); |
||||
return; |
||||
} |
||||
$this->instance=new OC_Cache_APC(); |
||||
} |
||||
} |
@ -1,31 +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/>. |
||||
* |
||||
*/ |
||||
|
||||
class Test_Cache_XCache extends Test_Cache { |
||||
public function setUp() { |
||||
if(!function_exists('xcache_get')) { |
||||
$this->markTestSkipped('The xcache extension is not available.'); |
||||
return; |
||||
} |
||||
$this->instance=new OC_Cache_XCache(); |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 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 Test\Memcache; |
||||
|
||||
class APC extends Cache { |
||||
public function setUp() { |
||||
if(!\OC\Memcache\APC::isAvailable()) { |
||||
$this->markTestSkipped('The apc extension is not available.'); |
||||
return; |
||||
} |
||||
$this->instance=new \OC\Memcache\APC(uniqid()); |
||||
} |
||||
} |
@ -0,0 +1,58 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 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 Test\Memcache; |
||||
|
||||
class Cache extends \Test_Cache { |
||||
public function testExistsAfterSet() { |
||||
$this->assertFalse($this->instance->hasKey('foo')); |
||||
$this->instance->set('foo', 'bar'); |
||||
$this->assertTrue($this->instance->hasKey('foo')); |
||||
} |
||||
|
||||
public function testGetAfterSet() { |
||||
$this->assertNull($this->instance->get('foo')); |
||||
$this->instance->set('foo', 'bar'); |
||||
$this->assertEquals('bar', $this->instance->get('foo')); |
||||
} |
||||
|
||||
public function testDoesNotExistAfterRemove() { |
||||
$this->instance->set('foo', 'bar'); |
||||
$this->instance->remove('foo'); |
||||
$this->assertFalse($this->instance->hasKey('foo')); |
||||
} |
||||
|
||||
public function testArrayAccessSet() { |
||||
$this->instance['foo'] = 'bar'; |
||||
$this->assertEquals('bar', $this->instance->get('foo')); |
||||
} |
||||
|
||||
public function testArrayAccessGet() { |
||||
$this->instance->set('foo', 'bar'); |
||||
$this->assertEquals('bar', $this->instance['foo']); |
||||
} |
||||
|
||||
public function testArrayAccessExists() { |
||||
$this->assertFalse(isset($this->instance['foo'])); |
||||
$this->instance->set('foo', 'bar'); |
||||
$this->assertTrue(isset($this->instance['foo'])); |
||||
} |
||||
|
||||
public function testArrayAccessUnset() { |
||||
$this->instance->set('foo', 'bar'); |
||||
unset($this->instance['foo']); |
||||
$this->assertFalse($this->instance->hasKey('foo')); |
||||
} |
||||
|
||||
public function tearDown() { |
||||
if ($this->instance) { |
||||
$this->instance->clear(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 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 Test\Memcache; |
||||
|
||||
class Memcached extends Cache { |
||||
public function setUp() { |
||||
if (!\OC\Memcache\Memcached::isAvailable()) { |
||||
$this->markTestSkipped('The memcached extension is not available.'); |
||||
return; |
||||
} |
||||
$this->instance = new \OC\Memcache\Memcached(uniqid()); |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 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 Test\Memcache; |
||||
|
||||
class XCache extends Cache { |
||||
public function setUp() { |
||||
if (!\OC\Memcache\XCache::isAvailable()) { |
||||
$this->markTestSkipped('The xcache extension is not available.'); |
||||
return; |
||||
} |
||||
$this->instance = new \OC\Memcache\XCache(uniqid()); |
||||
} |
||||
} |
Loading…
Reference in new issue