From 031a4c747e3e22a1ebf6db95306603a5a1118c66 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 11 Jul 2018 14:29:27 +0100 Subject: [PATCH] Add $maxAge parameter to MapCacheLRU get() and getField() methods Change-Id: I2dee1ad3a5b4f2ef8c182e3466812777a9a9cf7f --- includes/libs/MapCacheLRU.php | 25 +++++++++++++------ .../phpunit/includes/libs/MapCacheLRUTest.php | 5 ++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php index e891c9ed13..ad5e58d808 100644 --- a/includes/libs/MapCacheLRU.php +++ b/includes/libs/MapCacheLRU.php @@ -135,7 +135,7 @@ class MapCacheLRU implements IExpiringStore, Serializable { * Check if a key exists * * @param string $key - * @param float $maxAge Ignore items older than this many seconds (since 1.32) + * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) * @return bool */ public function has( $key, $maxAge = 0.0 ) { @@ -157,10 +157,11 @@ class MapCacheLRU implements IExpiringStore, Serializable { * If the item is already set, it will be pushed to the top of the cache. * * @param string $key - * @return mixed Returns null if the key was not found + * @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 */ - public function get( $key ) { - if ( !$this->has( $key ) ) { + public function get( $key, $maxAge = 0.0 ) { + if ( !$this->has( $key, $maxAge ) ) { return null; } @@ -193,7 +194,7 @@ class MapCacheLRU implements IExpiringStore, Serializable { /** * @param string|int $key * @param string|int $field - * @param float $maxAge + * @param float $maxAge Ignore items older than this many seconds [optional] (since 1.32) * @return bool */ public function hasField( $key, $field, $maxAge = 0.0 ) { @@ -205,8 +206,18 @@ class MapCacheLRU implements IExpiringStore, Serializable { return ( $maxAge <= 0 || $this->getAge( $key, $field ) <= $maxAge ); } - public function getField( $key, $field ) { - return $this->get( $key )[$field] ?? null; + /** + * @param string|int $key + * @param string|int $field + * @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 + */ + public function getField( $key, $field, $maxAge = 0.0 ) { + if ( !$this->hasField( $key, $field, $maxAge ) ) { + return null; + } + + return $this->cache[$key][$field]; } /** diff --git a/tests/phpunit/includes/libs/MapCacheLRUTest.php b/tests/phpunit/includes/libs/MapCacheLRUTest.php index 79f7b96f6e..a06ef62dfc 100644 --- a/tests/phpunit/includes/libs/MapCacheLRUTest.php +++ b/tests/phpunit/includes/libs/MapCacheLRUTest.php @@ -158,10 +158,12 @@ class MapCacheLRUTest extends PHPUnit\Framework\TestCase { $now += 29; $this->assertTrue( $cache->has( 'd', 30 ) ); $this->assertEquals( 'xxx', $cache->get( 'd' ) ); + $this->assertEquals( 'xxx', $cache->get( 'd', 30 ) ); $now += 1.5; $this->assertFalse( $cache->has( 'd', 30 ) ); $this->assertEquals( 'xxx', $cache->get( 'd' ) ); + $this->assertNull( $cache->get( 'd', 30 ) ); } /** @@ -180,14 +182,17 @@ class MapCacheLRUTest extends PHPUnit\Framework\TestCase { $cache->setField( 'PMs', 'Margaret Thatcher', 'Tory' ); $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) ); $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) ); + $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) ); $now += 29; $this->assertTrue( $cache->hasField( 'PMs', 'Tony Blair', 30 ) ); $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) ); + $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair', 30 ) ); $now += 1.5; $this->assertFalse( $cache->hasField( 'PMs', 'Tony Blair', 30 ) ); $this->assertEquals( 'Labour', $cache->getField( 'PMs', 'Tony Blair' ) ); + $this->assertNull( $cache->getField( 'PMs', 'Tony Blair', 30 ) ); $this->assertEquals( [ 'Tony Blair' => 'Labour', 'Margaret Thatcher' => 'Tory' ], -- 2.20.1