Merge "Made BagOStuff::cas properly optional"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 23 Feb 2015 05:32:38 +0000 (05:32 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 23 Feb 2015 05:32:38 +0000 (05:32 +0000)
includes/objectcache/APCBagOStuff.php
includes/objectcache/BagOStuff.php
includes/objectcache/EmptyBagOStuff.php
includes/objectcache/HashBagOStuff.php
includes/objectcache/MemcachedBagOStuff.php
includes/objectcache/MultiWriteBagOStuff.php
includes/objectcache/RedisBagOStuff.php
includes/objectcache/SqlBagOStuff.php
includes/objectcache/WinCacheBagOStuff.php
includes/objectcache/XCacheBagOStuff.php

index afc0f0a..eaf1155 100644 (file)
  * @ingroup Cache
  */
 class APCBagOStuff extends BagOStuff {
-       /**
-        * @param string $key
-        * @param int $casToken [optional]
-        * @return mixed
-        */
        public function get( $key, &$casToken = null ) {
                $val = apc_fetch( $key );
 
@@ -48,12 +43,6 @@ class APCBagOStuff extends BagOStuff {
                return $val;
        }
 
-       /**
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @return bool
-        */
        public function set( $key, $value, $exptime = 0 ) {
                if ( !$this->isInteger( $value ) ) {
                        $value = serialize( $value );
@@ -64,40 +53,12 @@ class APCBagOStuff extends BagOStuff {
                return true;
        }
 
-       /**
-        * @param mixed $casToken
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @return bool
-        * @throws MWException
-        */
-       protected function cas( $casToken, $key, $value, $exptime = 0 ) {
-               // APC's CAS functions only work on integers
-               throw new MWException( "CAS is not implemented in " . __CLASS__ );
-       }
-
-       /**
-        * @param string $key
-        * @return bool
-        */
        public function delete( $key ) {
                apc_delete( $key );
 
                return true;
        }
 
-       /**
-        * @param string $key
-        * @param callable $callback Callback method to be executed
-        * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
-        * @param int $attempts The amount of times to attempt a merge in case of failure
-        * @return bool Success
-        */
-       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
-               return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
-       }
-
        public function incr( $key, $value = 1 ) {
                return apc_inc( $key, $value );
        }
index 2c10742..5f0b4e2 100644 (file)
@@ -123,7 +123,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                        throw new Exception( "Got invalid callback." );
                }
 
-               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+               return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
        }
 
        /**
@@ -157,14 +157,17 @@ abstract class BagOStuff implements LoggerAwareInterface {
        }
 
        /**
-        * Check and set an item.
+        * Check and set an item
+        *
         * @param mixed $casToken
         * @param string $key
         * @param mixed $value
         * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
         * @return bool Success
         */
-       abstract protected function cas( $casToken, $key, $value, $exptime = 0 );
+       protected function cas( $casToken, $key, $value, $exptime = 0 ) {
+               throw new MWException( "CAS is not implemented in " . __CLASS__ );
+       }
 
        /**
         * @see BagOStuff::merge()
index 0fc65d9..4ccf270 100644 (file)
  * @ingroup Cache
  */
 class EmptyBagOStuff extends BagOStuff {
-
-       /**
-        * @param string $key
-        * @param mixed $casToken [optional]
-        * @return bool
-        */
        public function get( $key, &$casToken = null ) {
                return false;
        }
 
-       /**
-        * @param string $key
-        * @param mixed $value
-        * @param int $exp
-        * @return bool
-        */
        public function set( $key, $value, $exp = 0 ) {
                return true;
        }
 
-       /**
-        * @param mixed $casToken
-        * @param string $key
-        * @param mixed $value
-        * @param int $exp
-        * @return bool
-        */
-       protected function cas( $casToken, $key, $value, $exp = 0 ) {
-               return true;
-       }
-
-       /**
-        * @param string $key
-        * @return bool
-        */
        public function delete( $key ) {
                return true;
        }
 
-       /**
-        * @param string $key
-        * @param callable $callback Callback method to be executed
-        * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
-        * @param int $attempts The amount of times to attempt a merge in case of failure
-        * @return bool Success
-        */
        public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
-               return true;
+               return true; // faster
        }
 }
index 278a74e..9d65e38 100644 (file)
@@ -36,10 +36,6 @@ class HashBagOStuff extends BagOStuff {
                $this->bag = array();
        }
 
-       /**
-        * @param string $key
-        * @return bool
-        */
        protected function expire( $key ) {
                $et = $this->bag[$key][1];
 
@@ -52,11 +48,6 @@ class HashBagOStuff extends BagOStuff {
                return true;
        }
 
-       /**
-        * @param string $key
-        * @param mixed $casToken [optional]
-        * @return bool|mixed
-        */
        public function get( $key, &$casToken = null ) {
                if ( !isset( $this->bag[$key] ) ) {
                        return false;
@@ -71,36 +62,11 @@ class HashBagOStuff extends BagOStuff {
                return $this->bag[$key][0];
        }
 
-       /**
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @return bool
-        */
        public function set( $key, $value, $exptime = 0 ) {
                $this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
                return true;
        }
 
-       /**
-        * @param mixed $casToken
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @return bool
-        */
-       protected function cas( $casToken, $key, $value, $exptime = 0 ) {
-               if ( $this->get( $key ) === $casToken ) {
-                       return $this->set( $key, $value, $exptime );
-               }
-
-               return false;
-       }
-
-       /**
-        * @param string $key
-        * @return bool
-        */
        function delete( $key ) {
                if ( !isset( $this->bag[$key] ) ) {
                        return false;
@@ -110,4 +76,12 @@ class HashBagOStuff extends BagOStuff {
 
                return true;
        }
+
+       function lock( $key ) {
+               return true;
+       }
+
+       function unlock( $key ) {
+               return true;
+       }
 }
index ac34570..83bee70 100644 (file)
@@ -108,6 +108,14 @@ class MemcachedBagOStuff extends BagOStuff {
                        $this->fixExpiry( $exptime ) );
        }
 
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+               if ( !is_callable( $callback ) ) {
+                       throw new Exception( "Got invalid callback." );
+               }
+
+               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+       }
+
        /**
         * Get the underlying client object. This is provided for debugging
         * purposes.
index f9a8cfe..896eaf0 100644 (file)
@@ -76,18 +76,6 @@ class MultiWriteBagOStuff extends BagOStuff {
                return false;
        }
 
-       /**
-        * @param mixed $casToken
-        * @param string $key
-        * @param mixed $value
-        * @param mixed $exptime
-        * @return bool
-        * @throws MWException
-        */
-       protected function cas( $casToken, $key, $value, $exptime = 0 ) {
-               throw new MWException( "CAS is not implemented in " . __CLASS__ );
-       }
-
        /**
         * @param string $key
         * @param mixed $value
index b1be9d8..de3511d 100644 (file)
@@ -315,6 +315,15 @@ class RedisBagOStuff extends BagOStuff {
                $this->logRequest( 'incr', $key, $server, $result );
                return $result;
        }
+
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+               if ( !is_callable( $callback ) ) {
+                       throw new Exception( "Got invalid callback." );
+               }
+
+               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+       }
+
        /**
         * @param mixed $data
         * @return string
index b9a9985..df878f7 100644 (file)
@@ -514,6 +514,14 @@ class SqlBagOStuff extends BagOStuff {
                return $newValue;
        }
 
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+               if ( !is_callable( $callback ) ) {
+                       throw new Exception( "Got invalid callback." );
+               }
+
+               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+       }
+
        /**
         * @param DatabaseBase $db
         * @param string $exptime
index f59ed4e..5362574 100644 (file)
@@ -88,4 +88,12 @@ class WinCacheBagOStuff extends BagOStuff {
 
                return true;
        }
+
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+               if ( !is_callable( $callback ) ) {
+                       throw new Exception( "Got invalid callback." );
+               }
+
+               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+       }
 }
index 9be6624..cfee923 100644 (file)
@@ -68,19 +68,6 @@ class XCacheBagOStuff extends BagOStuff {
                return true;
        }
 
-       /**
-        * @param mixed $casToken
-        * @param string $key
-        * @param mixed $value
-        * @param int $exptime
-        * @return bool
-        * @throws MWException
-        */
-       protected function cas( $casToken, $key, $value, $exptime = 0 ) {
-               // Can't find any documentation on xcache cas
-               throw new MWException( "CAS is not implemented in " . __CLASS__ );
-       }
-
        /**
         * Remove a value from the XCache object cache
         *
@@ -92,21 +79,6 @@ class XCacheBagOStuff extends BagOStuff {
                return true;
        }
 
-       /**
-        * Merge an item.
-        * XCache does not seem to support any way of performing CAS - this however will
-        * provide a way to perform CAS-like functionality.
-        *
-        * @param string $key
-        * @param callable $callback Callback method to be executed
-        * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
-        * @param int $attempts The amount of times to attempt a merge in case of failure
-        * @return bool Success
-        */
-       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
-               return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
-       }
-
        public function incr( $key, $value = 1 ) {
                return xcache_inc( $key, $value );
        }