From: Aaron Schulz Date: Thu, 11 Jul 2019 11:32:35 +0000 (-0700) Subject: objectcache: normalize BagOStuff method overriding pattern for *Multi() methods X-Git-Tag: 1.34.0-rc.0~1072^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dcompta/comptes/journal.php?a=commitdiff_plain;h=383460afa3b78e46a6711da73ba7258652a10a7f;p=lhc%2Fweb%2Fwiklou.git objectcache: normalize BagOStuff method overriding pattern for *Multi() methods Change-Id: I1bebb60307b1a166461cb5f9a55a79194cc0e363 --- diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php index d13626ae1b..c47f6eeabc 100644 --- a/includes/libs/objectcache/BagOStuff.php +++ b/includes/libs/objectcache/BagOStuff.php @@ -407,7 +407,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param int $flags Bitfield of BagOStuff::WRITE_* constants * @return bool Success */ - protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { + final protected function mergeViaCas( $key, callable $callback, $exptime, $attempts, $flags ) { do { $casToken = null; // passed by reference // Get the old value and CAS token from cache @@ -665,19 +665,18 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar /** * Delete all objects expiring before a certain date. * @param string|int $timestamp The reference date in MW or TS_UNIX format - * @param callable|null $progressCallback Optional, a function which will be called + * @param callable|null $progress Optional, a function which will be called * regularly during long-running operations with the percentage progress * as the first parameter. [optional] * @param int $limit Maximum number of keys to delete [default: INF] * - * @return bool Success, false if unimplemented + * @return bool Success; false if unimplemented */ public function deleteObjectsExpiringBefore( $timestamp, - callable $progressCallback = null, + callable $progress = null, $limit = INF ) { - // stub return false; } @@ -726,11 +725,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @return bool Success * @since 1.24 */ - final public function setMulti( array $data, $exptime = 0, $flags = 0 ) { + public function setMulti( array $data, $exptime = 0, $flags = 0 ) { if ( ( $flags & self::WRITE_ALLOW_SEGMENTS ) === self::WRITE_ALLOW_SEGMENTS ) { throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' ); } - return $this->doSetMulti( $data, $exptime, $flags ); } @@ -745,7 +743,6 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar foreach ( $data as $key => $value ) { $res = $this->doSet( $key, $value, $exptime, $flags ) && $res; } - return $res; } @@ -759,11 +756,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @return bool Success * @since 1.33 */ - final public function deleteMulti( array $keys, $flags = 0 ) { + public function deleteMulti( array $keys, $flags = 0 ) { if ( ( $flags & self::WRITE_ALLOW_SEGMENTS ) === self::WRITE_ALLOW_SEGMENTS ) { throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' ); } - return $this->doDeleteMulti( $keys, $flags ); } @@ -777,7 +773,6 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar foreach ( $keys as $key ) { $res = $this->doDelete( $key, $flags ) && $res; } - return $res; } @@ -853,7 +848,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param mixed $mainValue * @return string|null|bool The combined string, false if missing, null on error */ - protected function resolveSegments( $key, $mainValue ) { + final protected function resolveSegments( $key, $mainValue ) { if ( SerializedValueContainer::isUnified( $mainValue ) ) { return $this->unserialize( $mainValue->{SerializedValueContainer::UNIFIED_DATA} ); } @@ -929,7 +924,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param callable $workCallback * @since 1.28 */ - public function addBusyCallback( callable $workCallback ) { + final public function addBusyCallback( callable $workCallback ) { $this->busyCallbacks[] = $workCallback; } @@ -938,9 +933,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar */ protected function debug( $text ) { if ( $this->debugMode ) { - $this->logger->debug( "{class} debug: $text", [ - 'class' => static::class, - ] ); + $this->logger->debug( "{class} debug: $text", [ 'class' => static::class ] ); } } @@ -948,7 +941,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param int $exptime * @return bool */ - protected function expiryIsRelative( $exptime ) { + final protected function expiryIsRelative( $exptime ) { return ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ); } @@ -964,9 +957,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param int $exptime Absolute TTL or 0 for indefinite * @return int */ - protected function convertToExpiry( $exptime ) { - $exptime = (int)$exptime; // sanity - + final protected function convertToExpiry( $exptime ) { return $this->expiryIsRelative( $exptime ) ? (int)$this->getCurrentTime() + $exptime : $exptime; @@ -979,16 +970,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param int $exptime * @return int */ - protected function convertToRelative( $exptime ) { - if ( $exptime >= ( 10 * self::TTL_YEAR ) ) { - $exptime -= (int)$this->getCurrentTime(); - if ( $exptime <= 0 ) { - $exptime = 1; - } - return $exptime; - } else { - return $exptime; - } + final protected function convertToRelative( $exptime ) { + return $this->expiryIsRelative( $exptime ) + ? (int)$exptime + : max( $exptime - (int)$this->getCurrentTime(), 1 ); } /** @@ -997,7 +982,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param mixed $value * @return bool */ - protected function isInteger( $value ) { + final protected function isInteger( $value ) { if ( is_int( $value ) ) { return true; } elseif ( !is_string( $value ) ) { @@ -1080,7 +1065,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar * @param BagOStuff[] $bags * @return int[] Resulting flag map (class ATTR_* constant => class QOS_* constant) */ - protected function mergeFlagMaps( array $bags ) { + final protected function mergeFlagMaps( array $bags ) { $map = []; foreach ( $bags as $bag ) { foreach ( $bag->attrMap as $attr => $rank ) { diff --git a/includes/libs/objectcache/CachedBagOStuff.php b/includes/libs/objectcache/CachedBagOStuff.php index 50ee1e3fdf..ea434e0c74 100644 --- a/includes/libs/objectcache/CachedBagOStuff.php +++ b/includes/libs/objectcache/CachedBagOStuff.php @@ -83,16 +83,12 @@ class CachedBagOStuff extends BagOStuff { public function deleteObjectsExpiringBefore( $timestamp, - callable $progressCallback = null, + callable $progress = null, $limit = INF ) { - $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit ); + $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ); - return $this->backend->deleteObjectsExpiringBefore( - $timestamp, - $progressCallback, - $limit - ); + return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ); } // These just call the backend (tested elsewhere) diff --git a/includes/libs/objectcache/EmptyBagOStuff.php b/includes/libs/objectcache/EmptyBagOStuff.php index 575bc58746..6dc1363ff7 100644 --- a/includes/libs/objectcache/EmptyBagOStuff.php +++ b/includes/libs/objectcache/EmptyBagOStuff.php @@ -33,7 +33,7 @@ class EmptyBagOStuff extends BagOStuff { return false; } - protected function doSet( $key, $value, $exp = 0, $flags = 0 ) { + protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { return true; } diff --git a/includes/libs/objectcache/MemcachedPeclBagOStuff.php b/includes/libs/objectcache/MemcachedPeclBagOStuff.php index f6721ce49d..221bc82ce3 100644 --- a/includes/libs/objectcache/MemcachedPeclBagOStuff.php +++ b/includes/libs/objectcache/MemcachedPeclBagOStuff.php @@ -261,7 +261,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff { return $result; } - public function doGetMulti( array $keys, $flags = 0 ) { + protected function doGetMulti( array $keys, $flags = 0 ) { $this->debug( 'getMulti(' . implode( ', ', $keys ) . ')' ); foreach ( $keys as $key ) { $this->validateKeyEncoding( $key ); @@ -270,7 +270,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff { return $this->checkResult( false, $result ); } - public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { + protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { $this->debug( 'setMulti(' . implode( ', ', array_keys( $data ) ) . ')' ); foreach ( array_keys( $data ) as $key ) { $this->validateKeyEncoding( $key ); @@ -279,7 +279,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff { return $this->checkResult( false, $result ); } - public function doDeleteMulti( array $keys, $flags = 0 ) { + protected function doDeleteMulti( array $keys, $flags = 0 ) { $this->debug( 'deleteMulti(' . implode( ', ', $keys ) . ')' ); foreach ( $keys as $key ) { $this->validateKeyEncoding( $key ); diff --git a/includes/libs/objectcache/MemcachedPhpBagOStuff.php b/includes/libs/objectcache/MemcachedPhpBagOStuff.php index 5d82fee539..f8b91bca05 100644 --- a/includes/libs/objectcache/MemcachedPhpBagOStuff.php +++ b/includes/libs/objectcache/MemcachedPhpBagOStuff.php @@ -111,7 +111,7 @@ class MemcachedPhpBagOStuff extends MemcachedBagOStuff { ); } - public function doGetMulti( array $keys, $flags = 0 ) { + protected function doGetMulti( array $keys, $flags = 0 ) { foreach ( $keys as $key ) { $this->validateKeyEncoding( $key ); } diff --git a/includes/libs/objectcache/MultiWriteBagOStuff.php b/includes/libs/objectcache/MultiWriteBagOStuff.php index 2df8f0c630..8e791bae1c 100644 --- a/includes/libs/objectcache/MultiWriteBagOStuff.php +++ b/includes/libs/objectcache/MultiWriteBagOStuff.php @@ -210,12 +210,12 @@ class MultiWriteBagOStuff extends BagOStuff { public function deleteObjectsExpiringBefore( $timestamp, - callable $progressCallback = null, + callable $progress = null, $limit = INF ) { $ret = false; foreach ( $this->caches as $cache ) { - if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit ) ) { + if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ) ) { $ret = true; } } @@ -236,7 +236,7 @@ class MultiWriteBagOStuff extends BagOStuff { return $res; } - public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { + public function setMulti( array $data, $exptime = 0, $flags = 0 ) { return $this->doWrite( $this->cacheIndexes, $this->usesAsyncWritesGivenFlags( $flags ), @@ -245,7 +245,16 @@ class MultiWriteBagOStuff extends BagOStuff { ); } - public function doDeleteMulti( array $data, $flags = 0 ) { + public function deleteMulti( array $data, $flags = 0 ) { + return $this->doWrite( + $this->cacheIndexes, + $this->usesAsyncWritesGivenFlags( $flags ), + __FUNCTION__, + func_get_args() + ); + } + + public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) { return $this->doWrite( $this->cacheIndexes, $this->usesAsyncWritesGivenFlags( $flags ), @@ -370,11 +379,19 @@ class MultiWriteBagOStuff extends BagOStuff { throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); } + protected function doSetMulti( array $keys, $exptime = 0, $flags = 0 ) { + throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); + } + + protected function doDeleteMulti( array $keys, $flags = 0 ) { + throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); + } + protected function serialize( $value ) { throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); } - protected function unserialize( $value ) { + protected function unserialize( $blob ) { throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); } } diff --git a/includes/libs/objectcache/RedisBagOStuff.php b/includes/libs/objectcache/RedisBagOStuff.php index f67b887335..dd859adf24 100644 --- a/includes/libs/objectcache/RedisBagOStuff.php +++ b/includes/libs/objectcache/RedisBagOStuff.php @@ -106,15 +106,15 @@ class RedisBagOStuff extends BagOStuff { return $result; } - protected function doSet( $key, $value, $expiry = 0, $flags = 0 ) { + protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { list( $server, $conn ) = $this->getConnection( $key ); if ( !$conn ) { return false; } - $expiry = $this->convertToRelative( $expiry ); + $ttl = $this->convertToRelative( $exptime ); try { - if ( $expiry ) { - $result = $conn->setex( $key, $expiry, $this->serialize( $value ) ); + if ( $ttl ) { + $result = $conn->setex( $key, $ttl, $this->serialize( $value ) ); } else { // No expiry, that is very different from zero expiry in Redis $result = $conn->set( $key, $this->serialize( $value ) ); @@ -146,7 +146,7 @@ class RedisBagOStuff extends BagOStuff { return $result; } - public function doGetMulti( array $keys, $flags = 0 ) { + protected function doGetMulti( array $keys, $flags = 0 ) { $batches = []; $conns = []; foreach ( $keys as $key ) { @@ -185,7 +185,7 @@ class RedisBagOStuff extends BagOStuff { return $result; } - public function doSetMulti( array $data, $expiry = 0, $flags = 0 ) { + protected function doSetMulti( array $data, $expiry = 0, $flags = 0 ) { $batches = []; $conns = []; foreach ( $data as $key => $value ) { @@ -229,7 +229,7 @@ class RedisBagOStuff extends BagOStuff { return $result; } - public function doDeleteMulti( array $keys, $flags = 0 ) { + protected function doDeleteMulti( array $keys, $flags = 0 ) { $batches = []; $conns = []; foreach ( $keys as $key ) { diff --git a/includes/libs/objectcache/ReplicatedBagOStuff.php b/includes/libs/objectcache/ReplicatedBagOStuff.php index 8c2b3f685b..295ec30c22 100644 --- a/includes/libs/objectcache/ReplicatedBagOStuff.php +++ b/includes/libs/objectcache/ReplicatedBagOStuff.php @@ -110,14 +110,10 @@ class ReplicatedBagOStuff extends BagOStuff { public function deleteObjectsExpiringBefore( $timestamp, - callable $progressCallback = null, + callable $progress = null, $limit = INF ) { - return $this->writeStore->deleteObjectsExpiringBefore( - $timestamp, - $progressCallback, - $limit - ); + return $this->writeStore->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ); } public function getMulti( array $keys, $flags = 0 ) { @@ -126,14 +122,18 @@ class ReplicatedBagOStuff extends BagOStuff { : $this->readStore->getMulti( $keys, $flags ); } - public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { + public function setMulti( array $data, $exptime = 0, $flags = 0 ) { return $this->writeStore->setMulti( $data, $exptime, $flags ); } - public function doDeleteMulti( array $keys, $flags = 0 ) { + public function deleteMulti( array $keys, $flags = 0 ) { return $this->writeStore->deleteMulti( $keys, $flags ); } + public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) { + return $this->writeStore->changeTTLMulti( $keys, $exptime, $flags ); + } + public function incr( $key, $value = 1 ) { return $this->writeStore->incr( $key, $value ); } @@ -189,6 +189,14 @@ class ReplicatedBagOStuff extends BagOStuff { throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); } + protected function doSetMulti( array $keys, $exptime = 0, $flags = 0 ) { + throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); + } + + protected function doDeleteMulti( array $keys, $flags = 0 ) { + throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); + } + protected function serialize( $value ) { throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); } diff --git a/includes/libs/objectcache/WinCacheBagOStuff.php b/includes/libs/objectcache/WinCacheBagOStuff.php index 9d7e143682..d75b34437e 100644 --- a/includes/libs/objectcache/WinCacheBagOStuff.php +++ b/includes/libs/objectcache/WinCacheBagOStuff.php @@ -67,8 +67,8 @@ class WinCacheBagOStuff extends BagOStuff { return $success; } - protected function doSet( $key, $value, $expire = 0, $flags = 0 ) { - $result = wincache_ucache_set( $key, $this->serialize( $value ), $expire ); + protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { + $result = wincache_ucache_set( $key, $this->serialize( $value ), $exptime ); // false positive, wincache_ucache_set returns an empty array // in some circumstances. diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index 7fa0bfaf4e..a8c23d6158 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -340,7 +340,7 @@ class SqlBagOStuff extends BagOStuff { return $values; } - public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { + protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) { return $this->modifyMulti( $data, $exptime, $flags, self::$OP_SET ); } @@ -509,7 +509,7 @@ class SqlBagOStuff extends BagOStuff { return (bool)$db->affectedRows(); } - public function doDeleteMulti( array $keys, $flags = 0 ) { + protected function doDeleteMulti( array $keys, $flags = 0 ) { return $this->modifyMulti( array_fill_keys( $keys, null ), 0, @@ -565,7 +565,7 @@ class SqlBagOStuff extends BagOStuff { return $ok; } - public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) { + protected function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) { return $this->modifyMulti( array_fill_keys( $keys, null ), $exptime, @@ -634,7 +634,7 @@ class SqlBagOStuff extends BagOStuff { public function deleteObjectsExpiringBefore( $timestamp, - callable $progressCallback = null, + callable $progress = null, $limit = INF ) { /** @noinspection PhpUnusedLocalVariableInspection */ @@ -653,7 +653,7 @@ class SqlBagOStuff extends BagOStuff { $this->deleteServerObjectsExpiringBefore( $db, $timestamp, - $progressCallback, + $progress, $limit, $numServersDone, $keysDeletedCount