summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-04-29 16:34:36 +0200
committerRobin Appelman <icewind@owncloud.com>2015-04-30 14:48:39 +0200
commitacf30ede95f4b8986787f81edcf025a84237b7c5 (patch)
tree8bfd36e370c084793f98a6cf0366916ab27f66cb /lib
parent29213b6136a4b2f71e5f981e9bc08e3e76128d4e (diff)
downloadnextcloud-server-acf30ede95f4b8986787f81edcf025a84237b7c5.tar.gz
nextcloud-server-acf30ede95f4b8986787f81edcf025a84237b7c5.zip
add compare and swap to memcache
Diffstat (limited to 'lib')
-rw-r--r--lib/private/memcache/apc.php21
-rw-r--r--lib/private/memcache/arraycache.php16
-rw-r--r--lib/private/memcache/castrait.php56
-rw-r--r--lib/private/memcache/memcached.php2
-rw-r--r--lib/private/memcache/redis.php1
-rw-r--r--lib/private/memcache/xcache.php28
-rw-r--r--lib/public/imemcache.php10
7 files changed, 132 insertions, 2 deletions
diff --git a/lib/private/memcache/apc.php b/lib/private/memcache/apc.php
index ba16972e6d6..50b942e7297 100644
--- a/lib/private/memcache/apc.php
+++ b/lib/private/memcache/apc.php
@@ -27,6 +27,10 @@ namespace OC\Memcache;
use OCP\IMemcache;
class APC extends Cache implements IMemcache {
+ use CASTrait {
+ cas as casEmulated;
+ }
+
public function get($key) {
$result = apc_fetch($this->getPrefix() . $key, $success);
if (!$success) {
@@ -89,6 +93,23 @@ class APC extends Cache implements IMemcache {
return apc_dec($this->getPrefix() . $key, $step);
}
+ /**
+ * Compare and set
+ *
+ * @param string $key
+ * @param mixed $old
+ * @param mixed $new
+ * @return bool
+ */
+ public function cas($key, $old, $new) {
+ // apc only does cas for ints
+ if (is_int($old) and is_int($new)) {
+ return apc_cas($this->getPrefix() . $key, $old, $new);
+ } else {
+ return $this->casEmulated($key, $old, $new);
+ }
+ }
+
static public function isAvailable() {
if (!extension_loaded('apc')) {
return false;
diff --git a/lib/private/memcache/arraycache.php b/lib/private/memcache/arraycache.php
index 6db920a69a8..2b1b87a9eb3 100644
--- a/lib/private/memcache/arraycache.php
+++ b/lib/private/memcache/arraycache.php
@@ -131,6 +131,22 @@ class ArrayCache extends Cache implements IMemcache {
}
/**
+ * Compare and set
+ *
+ * @param string $key
+ * @param mixed $old
+ * @param mixed $new
+ * @return bool
+ */
+ public function cas($key, $old, $new) {
+ if ($this->get($key) === $old) {
+ return $this->set($key, $new);
+ } else {
+ return false;
+ }
+ }
+
+ /**
* {@inheritDoc}
*/
static public function isAvailable() {
diff --git a/lib/private/memcache/castrait.php b/lib/private/memcache/castrait.php
new file mode 100644
index 00000000000..c52538023fb
--- /dev/null
+++ b/lib/private/memcache/castrait.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Memcache;
+
+trait CASTrait {
+ abstract public function get($key);
+
+ abstract public function set($key, $value, $ttl = 0);
+
+ abstract public function remove($key);
+
+ abstract public function add($key, $value, $ttl = 0);
+
+ /**
+ * Compare and set
+ *
+ * @param string $key
+ * @param mixed $old
+ * @param mixed $new
+ * @return bool
+ */
+ public function cas($key, $old, $new) {
+ //no native cas, emulate with locking
+ if ($this->add($key . '_lock', true)) {
+ if ($this->get($key) === $old) {
+ $this->set($key, $new);
+ $this->remove($key . '_lock');
+ return true;
+ } else {
+ $this->remove($key . '_lock');
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+}
diff --git a/lib/private/memcache/memcached.php b/lib/private/memcache/memcached.php
index 9566e54c42b..cf1d651b551 100644
--- a/lib/private/memcache/memcached.php
+++ b/lib/private/memcache/memcached.php
@@ -27,6 +27,8 @@ namespace OC\Memcache;
use OCP\IMemcache;
class Memcached extends Cache implements IMemcache {
+ use CASTrait;
+
/**
* @var \Memcached $cache
*/
diff --git a/lib/private/memcache/redis.php b/lib/private/memcache/redis.php
index dc52c03422a..78d061404ef 100644
--- a/lib/private/memcache/redis.php
+++ b/lib/private/memcache/redis.php
@@ -26,6 +26,7 @@ namespace OC\Memcache;
use OCP\IMemcache;
class Redis extends Cache implements IMemcache {
+ use CASTrait;
/**
* @var \Redis $cache
diff --git a/lib/private/memcache/xcache.php b/lib/private/memcache/xcache.php
index 48b0bd8a289..3a5bd73d8ad 100644
--- a/lib/private/memcache/xcache.php
+++ b/lib/private/memcache/xcache.php
@@ -24,6 +24,7 @@
*/
namespace OC\Memcache;
+
use OCP\IMemcache;
/**
@@ -92,7 +93,6 @@ class XCache extends Cache implements IMemcache {
* @return int | bool
*/
public function inc($key, $step = 1) {
- $this->add($key, 0);
return xcache_inc($this->getPrefix() . $key, $step);
}
@@ -107,11 +107,35 @@ class XCache extends Cache implements IMemcache {
return xcache_dec($this->getPrefix() . $key, $step);
}
+ /**
+ * Compare and set
+ *
+ * @param string $key
+ * @param mixed $old
+ * @param mixed $new
+ * @return bool
+ */
+ public function cas($key, $old, $new) {
+ //no native cas, emulate with locking
+ if ($this->add($key . '_lock', true)) {
+ if ($this->get($key) === $old) {
+ $this->set($key, $new);
+ $this->remove($key . '_lock');
+ return true;
+ } else {
+ $this->remove($key . '_lock');
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+
static public function isAvailable() {
if (!extension_loaded('xcache')) {
return false;
}
- if (\OC::$CLI) {
+ if (\OC::$CLI && !getenv('XCACHE_TEST')) {
return false;
}
if (!function_exists('xcache_unset_by_prefix') && ini_get('xcache.admin.enable_auth')) {
diff --git a/lib/public/imemcache.php b/lib/public/imemcache.php
index bc7762f80f9..56a33c9572f 100644
--- a/lib/public/imemcache.php
+++ b/lib/public/imemcache.php
@@ -65,4 +65,14 @@ interface IMemcache extends ICache {
* @since 8.0.0
*/
public function dec($key, $step = 1);
+
+ /**
+ * Compare and set
+ *
+ * @param string $key
+ * @param mixed $old
+ * @param mixed $new
+ * @return bool
+ */
+ public function cas($key, $old, $new);
}