summaryrefslogtreecommitdiffstats
path: root/lib/private/memcache
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/memcache')
-rw-r--r--lib/private/memcache/apc.php67
-rw-r--r--lib/private/memcache/apcu.php28
-rw-r--r--lib/private/memcache/cache.php77
-rw-r--r--lib/private/memcache/factory.php69
-rw-r--r--lib/private/memcache/memcached.php76
-rw-r--r--lib/private/memcache/xcache.php60
6 files changed, 377 insertions, 0 deletions
diff --git a/lib/private/memcache/apc.php b/lib/private/memcache/apc.php
new file mode 100644
index 00000000000..575ee4427db
--- /dev/null
+++ b/lib/private/memcache/apc.php
@@ -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;
+ }
+}
diff --git a/lib/private/memcache/apcu.php b/lib/private/memcache/apcu.php
new file mode 100644
index 00000000000..ccc1aa6e562
--- /dev/null
+++ b/lib/private/memcache/apcu.php
@@ -0,0 +1,28 @@
+<?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 APCu extends APC {
+ public function clear($prefix = '') {
+ $ns = $this->getNamespace() . $prefix;
+ $ns = preg_quote($ns, '/');
+ $iter = new \APCIterator('/^'.$ns.'/');
+ return apc_delete($iter);
+ }
+
+ static public function isAvailable() {
+ if (!extension_loaded('apcu')) {
+ return false;
+ } elseif (!ini_get('apc.enable_cli') && \OC::$CLI) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+}
diff --git a/lib/private/memcache/cache.php b/lib/private/memcache/cache.php
new file mode 100644
index 00000000000..0ad1cc7ec03
--- /dev/null
+++ b/lib/private/memcache/cache.php
@@ -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);
+ }
+}
diff --git a/lib/private/memcache/factory.php b/lib/private/memcache/factory.php
new file mode 100644
index 00000000000..fde7d947567
--- /dev/null
+++ b/lib/private/memcache/factory.php
@@ -0,0 +1,69 @@
+<?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 (APCu::isAvailable()) {
+ return new APCu($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() || APCu::isAvailable() || APC::isAvailable() || Memcached::isAvailable();
+ }
+
+ /**
+ * get a in-server cache instance, will return null if no backend is available
+ *
+ * @param string $prefix
+ * @return \OC\Memcache\Cache
+ */
+ public static function createLowLatency($prefix = '') {
+ if (XCache::isAvailable()) {
+ return new XCache($prefix);
+ } elseif (APCu::isAvailable()) {
+ return new APCu($prefix);
+ } elseif (APC::isAvailable()) {
+ return new APC($prefix);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * check if there is a in-server backend available
+ *
+ * @return bool
+ */
+ public static function isAvailableLowLatency() {
+ return XCache::isAvailable() || APCu::isAvailable() || APC::isAvailable();
+ }
+
+
+}
diff --git a/lib/private/memcache/memcached.php b/lib/private/memcache/memcached.php
new file mode 100644
index 00000000000..978e6c2eff1
--- /dev/null
+++ b/lib/private/memcache/memcached.php
@@ -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');
+ }
+}
diff --git a/lib/private/memcache/xcache.php b/lib/private/memcache/xcache.php
new file mode 100644
index 00000000000..33de30562f9
--- /dev/null
+++ b/lib/private/memcache/xcache.php
@@ -0,0 +1,60 @@
+<?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 XCache extends Cache {
+ /**
+ * 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);
+ }
+}