]> source.dussan.org Git - nextcloud-server.git/commitdiff
Seperate memory based cache from OC_Cache
authorRobin Appelman <icewind@owncloud.com>
Sun, 17 Mar 2013 15:00:39 +0000 (16:00 +0100)
committerRobin Appelman <icewind@owncloud.com>
Sun, 17 Mar 2013 15:00:39 +0000 (16:00 +0100)
lib/cache.php
lib/cache/apc.php [deleted file]
lib/cache/xcache.php [deleted file]
lib/memcache/apc.php [new file with mode: 0644]
lib/memcache/cache.php [new file with mode: 0644]
lib/memcache/xcache.php [new file with mode: 0644]
tests/lib/cache/apc.php [deleted file]
tests/lib/cache/xcache.php [deleted file]
tests/lib/memcache/apc.php [new file with mode: 0644]
tests/lib/memcache/xcache.php [new file with mode: 0644]

index bc74ed83f8bf150b7c596b306a4df6de38867aaf..48b9964ba9d19dd0713f3b5d188e0daa9b20e4e0 100644 (file)
@@ -15,41 +15,14 @@ class OC_Cache {
         * @var OC_Cache $global_cache
         */
        static protected $global_cache;
-       /**
-        * @var OC_Cache $global_cache_fast
-        */
-       static protected $global_cache_fast;
-       /**
-        * @var OC_Cache $user_cache_fast
-        */
-       static protected $user_cache_fast;
-       static protected $isFast=null;
 
        /**
         * get the global cache
         * @return OC_Cache
         */
-       static public function getGlobalCache($fast=false) {
+       static public function getGlobalCache() {
                if (!self::$global_cache) {
-                       self::$global_cache_fast = null;
-                       if (!self::$global_cache_fast && function_exists('xcache_set')) {
-                               self::$global_cache_fast = new OC_Cache_XCache(true);
-                       }
-                       if (!self::$global_cache_fast && function_exists('apc_store')) {
-                               self::$global_cache_fast = new OC_Cache_APC(true);
-                       }
-
                        self::$global_cache = new OC_Cache_FileGlobal();
-                       if (self::$global_cache_fast) {
-                               self::$global_cache = new OC_Cache_Broker(self::$global_cache_fast, self::$global_cache);
-                       }
-               }
-               if($fast) {
-                       if(self::$global_cache_fast) {
-                               return self::$global_cache_fast;
-                       }else{
-                               return false;
-                       }
                }
                return self::$global_cache;
        }
@@ -58,34 +31,16 @@ class OC_Cache {
         * get the user cache
         * @return OC_Cache
         */
-       static public function getUserCache($fast=false) {
+       static public function getUserCache() {
                if (!self::$user_cache) {
-                       self::$user_cache_fast = null;
-                       if (!self::$user_cache_fast && function_exists('xcache_set')) {
-                               self::$user_cache_fast = new OC_Cache_XCache();
-                       }
-                       if (!self::$user_cache_fast && function_exists('apc_store')) {
-                               self::$user_cache_fast = new OC_Cache_APC();
-                       }
-
                        self::$user_cache = new OC_Cache_File();
-                       if (self::$user_cache_fast) {
-                               self::$user_cache = new OC_Cache_Broker(self::$user_cache_fast, self::$user_cache);
-                       }
-               }
-
-               if($fast) {
-                       if(self::$user_cache_fast) {
-                               return self::$user_cache_fast;
-                       }else{
-                               return false;
-                       }
                }
                return self::$user_cache;
        }
 
        /**
         * get a value from the user cache
+        * @param string $key
         * @return mixed
         */
        static public function get($key) {
@@ -95,6 +50,9 @@ class OC_Cache {
 
        /**
         * set a value in the user cache
+        * @param string $key
+        * @param mixed $value
+        * @param int $ttl
         * @return bool
         */
        static public function set($key, $value, $ttl=0) {
@@ -107,6 +65,7 @@ class OC_Cache {
 
        /**
         * check if a value is set in the user cache
+        * @param string $key
         * @return bool
         */
        static public function hasKey($key) {
@@ -116,6 +75,7 @@ class OC_Cache {
 
        /**
         * remove an item from the user cache
+        * @param string $key
         * @return bool
         */
        static public function remove($key) {
@@ -133,17 +93,6 @@ class OC_Cache {
                return $user_cache->clear($prefix);
        }
 
-       /**
-        * check if a fast memory based cache is available
-        * @return true
-        */
-       static public function isFast() {
-               if(is_null(self::$isFast)) {
-                       self::$isFast=function_exists('xcache_set') || function_exists('apc_store');
-               }
-               return self::$isFast;
-       }
-
        static public function generateCacheKeyFromFiles($files) {
                $key = '';
                sort($files);
diff --git a/lib/cache/apc.php b/lib/cache/apc.php
deleted file mode 100644 (file)
index 895d307..0000000
+++ /dev/null
@@ -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;
-       }
-}
diff --git a/lib/cache/xcache.php b/lib/cache/xcache.php
deleted file mode 100644 (file)
index 9f380f8..0000000
+++ /dev/null
@@ -1,56 +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_XCache {
-       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='') {
-               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);
-                       }
-               }
-               xcache_unset_by_prefix($this->getNamespace().$prefix);
-               return true;
-       }
-}
diff --git a/lib/memcache/apc.php b/lib/memcache/apc.php
new file mode 100644 (file)
index 0000000..b3bb682
--- /dev/null
@@ -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 (file)
index 0000000..d77ea27
--- /dev/null
@@ -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 (file)
index 0000000..0ee34c6
--- /dev/null
@@ -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);
+       }
+}
diff --git a/tests/lib/cache/apc.php b/tests/lib/cache/apc.php
deleted file mode 100644 (file)
index bb5eb48..0000000
+++ /dev/null
@@ -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();
-       }
-}
diff --git a/tests/lib/cache/xcache.php b/tests/lib/cache/xcache.php
deleted file mode 100644 (file)
index 43bed2d..0000000
+++ /dev/null
@@ -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();
-       }
-}
diff --git a/tests/lib/memcache/apc.php b/tests/lib/memcache/apc.php
new file mode 100644 (file)
index 0000000..e3dccc0
--- /dev/null
@@ -0,0 +1,31 @@
+<?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_Memcache_APC extends Test_Cache {
+       public function setUp() {
+               if(!\OC\Memcache\APC::isAvailable()) {
+                       $this->markTestSkipped('The apc extension is not available.');
+                       return;
+               }
+               $this->instance=new \OC\Memcache\APC();
+       }
+}
diff --git a/tests/lib/memcache/xcache.php b/tests/lib/memcache/xcache.php
new file mode 100644 (file)
index 0000000..4877353
--- /dev/null
@@ -0,0 +1,31 @@
+<?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_Memcache_XCache extends Test_Cache {
+       public function setUp() {
+               if (!\OC\Memcache\XCache::isAvailable()) {
+                       $this->markTestSkipped('The xcache extension is not available.');
+                       return;
+               }
+               $this->instance = new \OC\Memcache\XCache();
+       }
+}