Made BagOStuff::merge() take any callable
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 10 Feb 2015 19:11:53 +0000 (11:11 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 11 Feb 2015 20:20:41 +0000 (12:20 -0800)
Change-Id: I5ce99b8d343f1629f888a3b7594976a83839e2b7

includes/objectcache/APCBagOStuff.php
includes/objectcache/BagOStuff.php
includes/objectcache/EmptyBagOStuff.php
includes/objectcache/MultiWriteBagOStuff.php
includes/objectcache/XCacheBagOStuff.php

index ae5dce0..afc0f0a 100644 (file)
@@ -89,12 +89,12 @@ class APCBagOStuff extends BagOStuff {
 
        /**
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
                return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
        }
 
index 0758aef..8712571 100644 (file)
@@ -113,12 +113,16 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * and takes the arguments: (this BagOStuff object, cache key, current value).
         *
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       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 );
        }
 
@@ -126,16 +130,17 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @see BagOStuff::merge()
         *
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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
         */
-       protected function mergeViaCas( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
                do {
                        $casToken = null; // passed by reference
-                       $currentValue = $this->get( $key, $casToken ); // get the old value
-                       $value = $callback( $this, $key, $currentValue ); // derive the new value
+                       $currentValue = $this->get( $key, $casToken );
+                       // Derive the new value from the old value
+                       $value = call_user_func( $callback, $this, $key, $currentValue );
 
                        if ( $value === false ) {
                                $success = true; // do nothing
@@ -165,18 +170,19 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @see BagOStuff::merge()
         *
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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
         */
-       protected function mergeViaLock( $key, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10 ) {
                if ( !$this->lock( $key, 6 ) ) {
                        return false;
                }
 
-               $currentValue = $this->get( $key ); // get the old value
-               $value = $callback( $this, $key, $currentValue ); // derive the new value
+               $currentValue = $this->get( $key );
+               // Derive the new value from the old value
+               $value = call_user_func( $callback, $this, $key, $currentValue );
 
                if ( $value === false ) {
                        $success = true; // do nothing
index 05a3d3f..0fc65d9 100644 (file)
@@ -68,12 +68,12 @@ class EmptyBagOStuff extends BagOStuff {
 
        /**
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
                return true;
        }
 }
index 9a32a27..f9a8cfe 100644 (file)
@@ -163,12 +163,12 @@ class MultiWriteBagOStuff extends BagOStuff {
 
        /**
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
                return $this->doWrite( 'merge', $key, $callback, $exptime );
        }
 
index a15e5c8..9be6624 100644 (file)
@@ -98,12 +98,12 @@ class XCacheBagOStuff extends BagOStuff {
         * provide a way to perform CAS-like functionality.
         *
         * @param string $key
-        * @param Closure $callback Callback method to be executed
+        * @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, Closure $callback, $exptime = 0, $attempts = 10 ) {
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
                return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
        }