[Pkg-owncloud-commits] [owncloud] 181/215: add compare and swap to memcache

David Prévot taffit at moszumanska.debian.org
Tue May 5 01:01:48 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit acf30ede95f4b8986787f81edcf025a84237b7c5
Author: Robin Appelman <icewind at owncloud.com>
Date:   Wed Apr 29 16:34:36 2015 +0200

    add compare and swap to memcache
---
 lib/private/memcache/apc.php        | 21 ++++++++++++++
 lib/private/memcache/arraycache.php | 16 +++++++++++
 lib/private/memcache/castrait.php   | 56 +++++++++++++++++++++++++++++++++++++
 lib/private/memcache/memcached.php  |  2 ++
 lib/private/memcache/redis.php      |  1 +
 lib/private/memcache/xcache.php     | 28 +++++++++++++++++--
 lib/public/imemcache.php            | 10 +++++++
 tests/lib/memcache/cache.php        | 12 ++++++++
 8 files changed, 144 insertions(+), 2 deletions(-)

diff --git a/lib/private/memcache/apc.php b/lib/private/memcache/apc.php
index ba16972..50b942e 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 6db920a..2b1b87a 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 0000000..c525380
--- /dev/null
+++ b/lib/private/memcache/castrait.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @author Robin Appelman <icewind at 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 9566e54..cf1d651 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 dc52c03..78d0614 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 48b0bd8..3a5bd73 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 bc7762f..56a33c9 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);
 }
diff --git a/tests/lib/memcache/cache.php b/tests/lib/memcache/cache.php
index 80ad182..9d977cf 100644
--- a/tests/lib/memcache/cache.php
+++ b/tests/lib/memcache/cache.php
@@ -91,6 +91,18 @@ abstract class Cache extends \Test_Cache {
 		$this->assertEquals('bar', $this->instance->get('foo'));
 	}
 
+	public function testCasNotChanged() {
+		$this->instance->set('foo', 'bar');
+		$this->assertTrue($this->instance->cas('foo', 'bar', 'asd'));
+		$this->assertEquals('asd', $this->instance->get('foo'));
+	}
+
+	public function testCasChanged() {
+		$this->instance->set('foo', 'bar1');
+		$this->assertFalse($this->instance->cas('foo', 'bar', 'asd'));
+		$this->assertEquals('bar1', $this->instance->get('foo'));
+	}
+
 
 	protected function tearDown() {
 		if ($this->instance) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list