From fd2c11c24b23cce7d9ee43225b19b75c7b5c6e8a Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 10 Feb 2015 11:11:53 -0800 Subject: [PATCH] Made BagOStuff::merge() take any callable Change-Id: I5ce99b8d343f1629f888a3b7594976a83839e2b7 --- includes/objectcache/APCBagOStuff.php | 4 +-- includes/objectcache/BagOStuff.php | 26 ++++++++++++-------- includes/objectcache/EmptyBagOStuff.php | 4 +-- includes/objectcache/MultiWriteBagOStuff.php | 4 +-- includes/objectcache/XCacheBagOStuff.php | 4 +-- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/includes/objectcache/APCBagOStuff.php b/includes/objectcache/APCBagOStuff.php index ae5dce0f3c..afc0f0acb8 100644 --- a/includes/objectcache/APCBagOStuff.php +++ b/includes/objectcache/APCBagOStuff.php @@ -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 ); } diff --git a/includes/objectcache/BagOStuff.php b/includes/objectcache/BagOStuff.php index 0758aef14d..87125713c5 100644 --- a/includes/objectcache/BagOStuff.php +++ b/includes/objectcache/BagOStuff.php @@ -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 diff --git a/includes/objectcache/EmptyBagOStuff.php b/includes/objectcache/EmptyBagOStuff.php index 05a3d3fe17..0fc65d910a 100644 --- a/includes/objectcache/EmptyBagOStuff.php +++ b/includes/objectcache/EmptyBagOStuff.php @@ -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; } } diff --git a/includes/objectcache/MultiWriteBagOStuff.php b/includes/objectcache/MultiWriteBagOStuff.php index 9a32a27013..f9a8cfe274 100644 --- a/includes/objectcache/MultiWriteBagOStuff.php +++ b/includes/objectcache/MultiWriteBagOStuff.php @@ -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 ); } diff --git a/includes/objectcache/XCacheBagOStuff.php b/includes/objectcache/XCacheBagOStuff.php index a15e5c88b3..9be6624edb 100644 --- a/includes/objectcache/XCacheBagOStuff.php +++ b/includes/objectcache/XCacheBagOStuff.php @@ -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 ); } -- 2.20.1