aboutsummaryrefslogtreecommitdiffstats
path: root/lib/memcache
diff options
context:
space:
mode:
Diffstat (limited to 'lib/memcache')
-rw-r--r--lib/memcache/apc.php76
-rw-r--r--lib/memcache/cache.php69
-rw-r--r--lib/memcache/xcache.php69
3 files changed, 214 insertions, 0 deletions
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 @@
+<?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 {
+ 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 @@
+<?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 {
+ /**
+ * 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 @@
+<?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 {
+ 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);
+ }
+}