diff options
author | Andreas Fischer <bantu@owncloud.com> | 2013-12-14 18:32:48 +0100 |
---|---|---|
committer | Andreas Fischer <bantu@owncloud.com> | 2013-12-14 18:32:48 +0100 |
commit | c205d8d1c957ab5421a5864d183257f596336d2d (patch) | |
tree | a097ff1232f1283074c2cbd589fa1fddac037391 /lib/private/memcache | |
parent | d73285c1869591da74c148b577d780a73313fe90 (diff) | |
parent | 77b68505c2164330803ce5d5dcbb9fd07438e18d (diff) | |
download | nextcloud-server-c205d8d1c957ab5421a5864d183257f596336d2d.tar.gz nextcloud-server-c205d8d1c957ab5421a5864d183257f596336d2d.zip |
Merge remote-tracking branch 'owncloud/master' into fixing-3417-master
* owncloud/master: (1989 commits)
[tx-robot] updated from transifex
dont try to register background jobs if we haven't upgraded yet
adjust test
coding style
coding style
On webdav sesssions, loginname was compared to username which does not need to match necessarily
rely only on php DateTime to parse the db datetime string
LDAP: fix method behind save button on advancend and expert tabs, fixes at least Home Folder setinng
Fix webroot for update page
Update 3rdparty ref
update 3rdparty
toggle select all checkbox
remove unneeded ; in comment
LDAP: the browser shall not autofill userdn and password, usually login credentials are inserted. fixes #6283
Add test for having utf8 filenames in the cache
fix fallback overwriting result of getHome
[tx-robot] updated from transifex
fix smbclient directory listing parser
cache the home folder of a User
Send "SET NAMES utf8" to MySQL for PHP below 5.3.6
...
Conflicts:
lib/util.php
Diffstat (limited to 'lib/private/memcache')
-rw-r--r-- | lib/private/memcache/apc.php | 67 | ||||
-rw-r--r-- | lib/private/memcache/apcu.php | 28 | ||||
-rw-r--r-- | lib/private/memcache/cache.php | 77 | ||||
-rw-r--r-- | lib/private/memcache/factory.php | 69 | ||||
-rw-r--r-- | lib/private/memcache/memcached.php | 76 | ||||
-rw-r--r-- | lib/private/memcache/xcache.php | 69 |
6 files changed, 386 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..2dc4a3a6016 --- /dev/null +++ b/lib/private/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; + +/** + * See http://xcache.lighttpd.net/wiki/XcacheApi for provided constants and + * functions etc. + */ +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='') { + if (function_exists('xcache_unset_by_prefix')) { + return xcache_unset_by_prefix($this->getNamespace().$prefix); + } else { + // Since we can not clear by prefix, we just clear the whole cache. + xcache_clear_cache(\XC_TYPE_VAR, 0); + } + return true; + } + + static public function isAvailable(){ + if (!extension_loaded('xcache')) { + return false; + } + if (\OC::$CLI) { + return false; + } + if (!function_exists('xcache_unset_by_prefix') && ini_get('xcache.admin.enable_auth')) { + // We do not want to use XCache if we can not clear it without + // using the administration function xcache_clear_cache() + // AND administration functions are password-protected. + return false; + } + + return true; + } +} |