X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FMapCacheLRU.php;h=a49eb0106cbc10dc614acc8789c48fb8e4b666dc;hb=a2ea9f8b9289f197844e3282c2ac39e59c549996;hp=0b6db32ef992cb6e8814ec57d10c0dd4012b515f;hpb=bddb5409b5048c96dff9c000fa8b196add0145d6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/MapCacheLRU.php b/includes/libs/MapCacheLRU.php index 0b6db32ef9..a49eb0106c 100644 --- a/includes/libs/MapCacheLRU.php +++ b/includes/libs/MapCacheLRU.php @@ -20,6 +20,7 @@ * @file * @ingroup Cache */ +use Wikimedia\Assert\Assert; /** * Handles a simple LRU key/value map with a maximum number of entries @@ -38,12 +39,12 @@ class MapCacheLRU { /** * @param int $maxKeys Maximum number of entries allowed (min 1). - * @throws Exception When $maxCacheKeys is not an int or =< 0. + * @throws Exception When $maxCacheKeys is not an int or not above zero. */ public function __construct( $maxKeys ) { - if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { - throw new Exception( __METHOD__ . " must be given an integer and >= 1" ); - } + Assert::parameterType( 'integer', $maxKeys, '$maxKeys' ); + Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' ); + $this->maxCacheKeys = $maxKeys; } @@ -58,7 +59,7 @@ class MapCacheLRU { */ public function set( $key, $value ) { if ( array_key_exists( $key, $this->cache ) ) { - $this->ping( $key ); // push to top + $this->ping( $key ); } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) { reset( $this->cache ); $evictKey = key( $this->cache ); @@ -86,12 +87,11 @@ class MapCacheLRU { * @return mixed */ public function get( $key ) { - if ( array_key_exists( $key, $this->cache ) ) { - $this->ping( $key ); // push to top - return $this->cache[$key]; - } else { + if ( !array_key_exists( $key, $this->cache ) ) { return null; } + $this->ping( $key ); + return $this->cache[$key]; } /**