From 250f2b9e5ad67e22274961dd3464589d1559eb08 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 18 Feb 2015 15:47:06 -0800 Subject: [PATCH] Made BagOStuff::cas properly optional * merge() using the locking version by default. The callers that implement cas() override merge() to use the cas-based version. Those that do not no longer need to define the unused dummy method. * Removed some redundant doc blocks. Change-Id: I49f7dd5432efa8d76f4758c273f0859376ddafb7 --- includes/objectcache/APCBagOStuff.php | 39 ------------------ includes/objectcache/BagOStuff.php | 9 +++-- includes/objectcache/EmptyBagOStuff.php | 36 +---------------- includes/objectcache/HashBagOStuff.php | 42 ++++---------------- includes/objectcache/MemcachedBagOStuff.php | 8 ++++ includes/objectcache/MultiWriteBagOStuff.php | 12 ------ includes/objectcache/RedisBagOStuff.php | 9 +++++ includes/objectcache/SqlBagOStuff.php | 8 ++++ includes/objectcache/WinCacheBagOStuff.php | 8 ++++ includes/objectcache/XCacheBagOStuff.php | 28 ------------- 10 files changed, 48 insertions(+), 151 deletions(-) diff --git a/includes/objectcache/APCBagOStuff.php b/includes/objectcache/APCBagOStuff.php index afc0f0acb8..eaf115570c 100644 --- a/includes/objectcache/APCBagOStuff.php +++ b/includes/objectcache/APCBagOStuff.php @@ -27,11 +27,6 @@ * @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 ); } diff --git a/includes/objectcache/BagOStuff.php b/includes/objectcache/BagOStuff.php index 2c10742c26..5f0b4e2dbf 100644 --- a/includes/objectcache/BagOStuff.php +++ b/includes/objectcache/BagOStuff.php @@ -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() diff --git a/includes/objectcache/EmptyBagOStuff.php b/includes/objectcache/EmptyBagOStuff.php index 0fc65d910a..4ccf2707ae 100644 --- a/includes/objectcache/EmptyBagOStuff.php +++ b/includes/objectcache/EmptyBagOStuff.php @@ -27,53 +27,19 @@ * @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 } } diff --git a/includes/objectcache/HashBagOStuff.php b/includes/objectcache/HashBagOStuff.php index 278a74e865..9d65e3891f 100644 --- a/includes/objectcache/HashBagOStuff.php +++ b/includes/objectcache/HashBagOStuff.php @@ -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; + } } diff --git a/includes/objectcache/MemcachedBagOStuff.php b/includes/objectcache/MemcachedBagOStuff.php index ac34570136..83bee700d3 100644 --- a/includes/objectcache/MemcachedBagOStuff.php +++ b/includes/objectcache/MemcachedBagOStuff.php @@ -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. diff --git a/includes/objectcache/MultiWriteBagOStuff.php b/includes/objectcache/MultiWriteBagOStuff.php index f9a8cfe274..896eaf009a 100644 --- a/includes/objectcache/MultiWriteBagOStuff.php +++ b/includes/objectcache/MultiWriteBagOStuff.php @@ -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 diff --git a/includes/objectcache/RedisBagOStuff.php b/includes/objectcache/RedisBagOStuff.php index b1be9d83d0..de3511df97 100644 --- a/includes/objectcache/RedisBagOStuff.php +++ b/includes/objectcache/RedisBagOStuff.php @@ -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 diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index b9a99853de..df878f7205 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -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 diff --git a/includes/objectcache/WinCacheBagOStuff.php b/includes/objectcache/WinCacheBagOStuff.php index f59ed4ea61..536257464e 100644 --- a/includes/objectcache/WinCacheBagOStuff.php +++ b/includes/objectcache/WinCacheBagOStuff.php @@ -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 ); + } } diff --git a/includes/objectcache/XCacheBagOStuff.php b/includes/objectcache/XCacheBagOStuff.php index 9be6624edb..cfee92362a 100644 --- a/includes/objectcache/XCacheBagOStuff.php +++ b/includes/objectcache/XCacheBagOStuff.php @@ -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 ); } -- 2.20.1