objectcache: update MemcachedPeclBagOStuff for pecl memcached 3.0.0
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 31 May 2018 21:41:02 +0000 (14:41 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 3 Jun 2018 00:25:22 +0000 (17:25 -0700)
The get() $cas_token parameter was changed into $flags, which can act
as a switch to make the return value an associative array of details.

pecl and PHP-based memcached BagOStuff class both pass all tests now.

Bug: T196125
Change-Id: I4678250331a48db4d50d1fc6c489c991a4dee920

includes/libs/objectcache/MemcachedPeclBagOStuff.php
includes/libs/objectcache/MemcachedPhpBagOStuff.php
tests/phpunit/includes/libs/objectcache/BagOStuffTest.php

index e3e66d5..fe31c25 100644 (file)
@@ -140,7 +140,19 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
 
        protected function getWithToken( $key, &$casToken, $flags = 0 ) {
                $this->debugLog( "get($key)" );
-               $result = $this->client->get( $this->validateKeyEncoding( $key ), null, $casToken );
+               if ( defined( Memcached::class . '::GET_EXTENDED' ) ) { // v3.0.0
+                       $flags = Memcached::GET_EXTENDED;
+                       $res = $this->client->get( $this->validateKeyEncoding( $key ), null, $flags );
+                       if ( is_array( $res ) ) {
+                               $result = $res['value'];
+                               $casToken = $res['cas'];
+                       } else {
+                               $result = false;
+                               $casToken = null;
+                       }
+               } else {
+                       $result = $this->client->get( $this->validateKeyEncoding( $key ), null, $casToken );
+               }
                $result = $this->checkResult( $key, $result );
                return $result;
        }
index e2d9d93..3ff390b 100644 (file)
@@ -62,12 +62,12 @@ class MemcachedPhpBagOStuff extends MemcachedBagOStuff {
        public function incr( $key, $value = 1 ) {
                $this->validateKeyEncoding( $key );
 
-               return $this->client->incr( $key, $value );
+               return $this->client->incr( $key, $value ) ?? false;
        }
 
        public function decr( $key, $value = 1 ) {
                $this->validateKeyEncoding( $key );
 
-               return $this->client->decr( $key, $value );
+               return $this->client->decr( $key, $value ) ?? false;
        }
 }
index d570297..b6709a0 100644 (file)
@@ -76,12 +76,12 @@ class BagOStuffTest extends MediaWikiTestCase {
                // merge on non-existing value
                $merged = $this->cache->merge( $key, $callback, 5 );
                $this->assertTrue( $merged );
-               $this->assertEquals( $this->cache->get( $key ), 'merged' );
+               $this->assertEquals( 'merged', $this->cache->get( $key ) );
 
                // merge on existing value
                $merged = $this->cache->merge( $key, $callback, 5 );
                $this->assertTrue( $merged );
-               $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
+               $this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
        }
 
        /**