From 80a3f8d066b7deffe70a232ca956746db02db138 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 17 Mar 2013 16:00:39 +0100 Subject: Seperate memory based cache from OC_Cache --- lib/memcache/apc.php | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/memcache/cache.php | 69 ++++++++++++++++++++++++++++++++++++++++++++ lib/memcache/xcache.php | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 lib/memcache/apc.php create mode 100644 lib/memcache/cache.php create mode 100644 lib/memcache/xcache.php (limited to 'lib/memcache') diff --git a/lib/memcache/apc.php b/lib/memcache/apc.php new file mode 100644 index 00000000000..b3bb68223b4 --- /dev/null +++ b/lib/memcache/apc.php @@ -0,0 +1,76 @@ + + * 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 { + 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; + } + + 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; + } +} diff --git a/lib/memcache/cache.php b/lib/memcache/cache.php new file mode 100644 index 00000000000..d77ea27933f --- /dev/null +++ b/lib/memcache/cache.php @@ -0,0 +1,69 @@ + + * 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 { + /** + * get a cache instance + * + * @param bool $global + * @return Cache + */ + static function create($global = false) { + if (XCache::isAvailable()) { + return new XCache($global); + } elseif (APC::isAvailable()) { + return new APC($global); + } else { + return null; + } + } + + /** + * @param bool $global + */ + abstract public function __construct($global); + + /** + * @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 = ''); + + /** + * @return bool + */ + //static public function isAvailable(); +} diff --git a/lib/memcache/xcache.php b/lib/memcache/xcache.php new file mode 100644 index 00000000000..0ee34c667d3 --- /dev/null +++ b/lib/memcache/xcache.php @@ -0,0 +1,69 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Memcache; + +class XCache extends Cache { + protected $prefix; + + public function __construct($global = false) { + $this->prefix = \OC_Util::getInstanceId().'/'; + if (!$global) { + $this->prefix .= \OC_User::getUser().'/'; + } + } + + /** + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users + */ + protected function getNameSpace() { + return $this->prefix; + } + + public function get($key) { + return xcache_get($this->getNamespace().$key); + } + + public function set($key, $value, $ttl=0) { + if($ttl>0) { + return xcache_set($this->getNamespace().$key, $value, $ttl); + }else{ + return xcache_set($this->getNamespace().$key, $value); + } + } + + public function hasKey($key) { + return xcache_isset($this->getNamespace().$key); + } + + public function remove($key) { + return xcache_unset($this->getNamespace().$key); + } + + public function clear($prefix='') { + xcache_unset_by_prefix($this->getNamespace().$prefix); + return true; + } + + static public function isAvailable(){ + if (!extension_loaded('xcache')) { + return false; + } elseif (\OC::$CLI) { + return false; + }else{ + return true; + } + } +} + +if(!function_exists('xcache_unset_by_prefix')) { + function xcache_unset_by_prefix($prefix) { + // Since we can't clear targetted cache, we'll clear all. :( + xcache_clear_cache(\XC_TYPE_VAR, 0); + } +} -- cgit v1.2.3