Merge "Permit case independent unit test directories"
[lhc/web/wiklou.git] / includes / libs / MapCacheLRU.php
index bca8c05..e0c81ed 100644 (file)
@@ -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) */
@@ -64,7 +65,7 @@ class MapCacheLRU implements IExpiringStore, Serializable {
                Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
 
                $this->maxCacheKeys = $maxKeys;
-               // Use the current time as the default "as of" timesamp of entries
+               // Use the current time as the default "as of" timestamp of entries
                $this->epoch = $this->getCurrentTime();
        }
 
@@ -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,13 +136,14 @@ 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 [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__ . ' called with invalid key. Must be string or integer.' );
+                               __METHOD__ . ': invalid key; must be string or integer.' );
                }
 
                if ( !array_key_exists( $key, $this->cache ) ) {
@@ -157,11 +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
-        * @return mixed Returns null if the key was not found
+        * @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 ) {
-               if ( !$this->has( $key ) ) {
-                       return null;
+       public function get( $key, $maxAge = INF, $default = null ) {
+               if ( !$this->has( $key, $maxAge ) ) {
+                       return $default;
                }
 
                $this->ping( $key );
@@ -182,8 +188,15 @@ class MapCacheLRU implements IExpiringStore, Serializable {
                        $this->set( $key, [], $initRank );
                }
 
+               if ( !is_int( $field ) && !is_string( $field ) ) {
+                       throw new UnexpectedValueException(
+                               __METHOD__ . ": invalid field for '$key'; must be string or integer." );
+               }
+
                if ( !is_array( $this->cache[$key] ) ) {
-                       throw new UnexpectedValueException( "The value of '$key' is not an array." );
+                       $type = gettype( $this->cache[$key] );
+
+                       throw new UnexpectedValueException( "The value of '$key' ($type) is not an array." );
                }
 
                $this->cache[$key][$field] = $value;
@@ -193,11 +206,18 @@ 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 [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 ) ) {
+                       throw new UnexpectedValueException(
+                               __METHOD__ . ": invalid field for '$key'; must be string or integer." );
+               }
+
                if ( !is_array( $value ) || !array_key_exists( $field, $value ) ) {
                        return false;
                }
@@ -205,8 +225,19 @@ 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 [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 = INF ) {
+               if ( !$this->hasField( $key, $field, $maxAge ) ) {
+                       return null;
+               }
+
+               return $this->cache[$key][$field];
        }
 
        /**
@@ -225,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 );
@@ -247,11 +279,11 @@ class MapCacheLRU implements IExpiringStore, Serializable {
        /**
         * Clear one or several cache entries, or all cache entries
         *
-        * @param string|array $keys
+        * @param string|array|null $keys
         * @return void
         */
        public function clear( $keys = null ) {
-               if ( $keys === null ) {
+               if ( func_num_args() == 0 ) {
                        $this->cache = [];
                        $this->timestamps = [];
                } else {