From 69ad84cf36fb7efc28c6d4c95c97dd1c1a8a2c97 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 9 Jul 2019 20:03:14 -0700 Subject: [PATCH] Make MapCacheLRU::get() accept a default value and improve $maxAge default Change-Id: If562e1745ffed37b0f1ed5382a5a397fc68aac8e --- includes/libs/MapCacheLRU.php | 36 +++++++++++-------- .../phpunit/includes/libs/MapCacheLRUTest.php | 15 ++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php index 3a549af7af..e0c81eddba 100644 --- a/includes/libs/MapCacheLRU.php +++ b/includes/libs/MapCacheLRU.php @@ -48,6 +48,7 @@ class MapCacheLRU implements IExpiringStore, Serializable { /** @var float|null */ private $wallClockOverride; + /** @var float */ const RANK_TOP = 1.0; /** @var int Array key that holds the entry's main timestamp (flat key use) */ @@ -103,7 +104,7 @@ class MapCacheLRU implements IExpiringStore, Serializable { * * @param string $key * @param mixed $value - * @param float $rank Bottom fraction of the list where keys start off [Default: 1.0] + * @param float $rank Bottom fraction of the list where keys start off [default: 1.0] * @return void */ public function set( $key, $value, $rank = self::RANK_TOP ) { @@ -135,10 +136,11 @@ class MapCacheLRU implements IExpiringStore, Serializable { * Check if a key exists * * @param string $key - * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) + * @param float $maxAge Ignore items older than this many seconds [default: INF] * @return bool + * @since 1.32 Added $maxAge */ - public function has( $key, $maxAge = 0.0 ) { + public function has( $key, $maxAge = INF ) { if ( !is_int( $key ) && !is_string( $key ) ) { throw new UnexpectedValueException( __METHOD__ . ': invalid key; must be string or integer.' ); @@ -157,12 +159,15 @@ class MapCacheLRU implements IExpiringStore, Serializable { * If the item is already set, it will be pushed to the top of the cache. * * @param string $key - * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) - * @return mixed Returns null if the key was not found or is older than $maxAge + * @param float $maxAge Ignore items older than this many seconds [default: INF] + * @param mixed|null $default Value to return if no key is found [default: null] + * @return mixed Returns $default if the key was not found or is older than $maxAge + * @since 1.32 Added $maxAge + * @since 1.34 Added $default */ - public function get( $key, $maxAge = 0.0 ) { + public function get( $key, $maxAge = INF, $default = null ) { if ( !$this->has( $key, $maxAge ) ) { - return null; + return $default; } $this->ping( $key ); @@ -201,10 +206,11 @@ class MapCacheLRU implements IExpiringStore, Serializable { /** * @param string|int $key * @param string|int $field - * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) + * @param float $maxAge Ignore items older than this many seconds [default: INF] * @return bool + * @since 1.32 Added $maxAge */ - public function hasField( $key, $field, $maxAge = 0.0 ) { + public function hasField( $key, $field, $maxAge = INF ) { $value = $this->get( $key ); if ( !is_int( $field ) && !is_string( $field ) ) { @@ -222,10 +228,11 @@ class MapCacheLRU implements IExpiringStore, Serializable { /** * @param string|int $key * @param string|int $field - * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) + * @param float $maxAge Ignore items older than this many seconds [default: INF] * @return mixed Returns null if the key was not found or is older than $maxAge + * @since 1.32 Added $maxAge */ - public function getField( $key, $field, $maxAge = 0.0 ) { + public function getField( $key, $field, $maxAge = INF ) { if ( !$this->hasField( $key, $field, $maxAge ) ) { return null; } @@ -249,12 +256,13 @@ class MapCacheLRU implements IExpiringStore, Serializable { * @since 1.28 * @param string $key * @param callable $callback Callback that will produce the value - * @param float $rank Bottom fraction of the list where keys start off [Default: 1.0] - * @param float $maxAge Ignore items older than this many seconds [Default: 0.0] (since 1.32) + * @param float $rank Bottom fraction of the list where keys start off [default: 1.0] + * @param float $maxAge Ignore items older than this many seconds [default: INF] * @return mixed The cached value if found or the result of $callback otherwise + * @since 1.32 Added $maxAge */ public function getWithSetCallback( - $key, callable $callback, $rank = self::RANK_TOP, $maxAge = 0.0 + $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF ) { if ( $this->has( $key, $maxAge ) ) { $value = $this->get( $key ); diff --git a/tests/phpunit/includes/libs/MapCacheLRUTest.php b/tests/phpunit/includes/libs/MapCacheLRUTest.php index 7147c6fa22..0c8dc68986 100644 --- a/tests/phpunit/includes/libs/MapCacheLRUTest.php +++ b/tests/phpunit/includes/libs/MapCacheLRUTest.php @@ -69,6 +69,21 @@ class MapCacheLRUTest extends PHPUnit\Framework\TestCase { ); } + /** + * @covers MapCacheLRU::has() + * @covers MapCacheLRU::get() + * @covers MapCacheLRU::set() + */ + function testMissing() { + $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; + $cache = MapCacheLRU::newFromArray( $raw, 3 ); + + $this->assertFalse( $cache->has( 'd' ) ); + $this->assertNull( $cache->get( 'd' ) ); + $this->assertNull( $cache->get( 'd', 0.0, null ) ); + $this->assertFalse( $cache->get( 'd', 0.0, false ) ); + } + /** * @covers MapCacheLRU::has() * @covers MapCacheLRU::get() -- 2.20.1