23use InvalidArgumentException;
24use UnexpectedValueException;
42private $timestamps = [];
50private $wallClockOverride;
53privateconst RANK_TOP = 1.0;
56privateconst SIMPLE = 0;
58privateconst FIELDS = 1;
65thrownew InvalidArgumentException(
'$maxKeys must be above zero' );
68 $this->maxCacheKeys = $maxKeys;
69// Use the current time as the default "as of" timestamp of entries 80 $mapCache =
newself( $maxKeys );
81 $mapCache->cache = ( count( $values ) > $maxKeys )
82 ? array_slice( $values, -$maxKeys,
null,
true )
111publicfunctionset( $key, $value, $rank = self::RANK_TOP ) {
112if ( $this->
has( $key ) ) {
114 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
115 $evictKey = array_key_first( $this->cache );
116 unset( $this->cache[$evictKey] );
117 unset( $this->timestamps[$evictKey] );
120if ( $rank < 1.0 && $rank > 0 ) {
121 $offset = intval( $rank * count( $this->cache ) );
122 $this->cache = array_slice( $this->cache, 0, $offset,
true )
124 + array_slice( $this->cache, $offset,
null,
true );
126 $this->cache[$key] = $value;
129 $this->timestamps[$key] = [
143publicfunctionhas( $key, $maxAge = INF ) {
144if ( !is_int( $key ) && !is_string( $key ) ) {
145thrownew UnexpectedValueException(
146 __METHOD__ .
': invalid key; must be string or integer.' );
148return array_key_exists( $key, $this->cache )
150// Optimization: Avoid expensive getAge/getCurrentTime for common case (T275673) 153 || $this->getKeyAge( $key ) <= $maxAge
173publicfunctionget( $key, $maxAge = INF, $default = null ) {
174if ( !$this->
has( $key, $maxAge ) ) {
180return $this->cache[$key];
189publicfunctionsetField( $key, $field, $value, $initRank = self::RANK_TOP ) {
190if ( $this->
has( $key ) ) {
193if ( !is_array( $this->cache[$key] ) ) {
194 $type = get_debug_type( $this->cache[$key] );
195thrownew UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
198 $this->
set( $key, [], $initRank );
201if ( !is_string( $field ) && !is_int( $field ) ) {
202thrownew UnexpectedValueException(
203 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
206 $this->cache[$key][$field] = $value;
207 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
217publicfunctionhasField( $key, $field, $maxAge = INF ) {
218 $value = $this->
get( $key );
220if ( !is_int( $field ) && !is_string( $field ) ) {
221thrownew UnexpectedValueException(
222 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
224return is_array( $value )
225 && array_key_exists( $field, $value )
229 || $this->getKeyFieldAge( $key, $field ) <= $maxAge
240publicfunctiongetField( $key, $field, $maxAge = INF ) {
241if ( !$this->
hasField( $key, $field, $maxAge ) ) {
245return $this->cache[$key][$field];
253return array_keys( $this->cache );
270 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
272if ( $this->
has( $key, $maxAge ) ) {
273 $value = $this->
get( $key );
275 $value = $callback();
276if ( $value !==
false ) {
277 $this->
set( $key, $value, $rank );
292// Based on BagOStuff::makeKeyInternal, except without a required 293// $keygroup prefix. While MapCacheLRU can and is used as cache for 294// multiple groups of keys, it is equally common for the instance itself 295// to represent a single group, and thus have keys where the first component 296// can directly be a user-controlled variable. 298foreach ( $components as $i => $component ) {
302 $key .= strtr( $component, [
'%' =>
'%25',
':' =>
'%3A' ] );
314if ( func_num_args() == 0 ) {
316 $this->timestamps = [];
318foreach ( (array)$keys as $key ) {
319 unset( $this->cache[$key] );
320 unset( $this->timestamps[$key] );
332return $this->maxCacheKeys;
343if ( $maxKeys <= 0 ) {
344thrownew InvalidArgumentException(
'$maxKeys must be above zero' );
347 $this->maxCacheKeys = $maxKeys;
348while ( count( $this->cache ) > $this->maxCacheKeys ) {
349 $evictKey = array_key_first( $this->cache );
350 unset( $this->cache[$evictKey] );
351 unset( $this->timestamps[$evictKey] );
360privatefunction ping( $key ) {
361 $item = $this->cache[$key];
362 unset( $this->cache[$key] );
363 $this->cache[$key] = $item;
370privatefunction getKeyAge( $key ) {
371 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
381privatefunction getKeyFieldAge( $key, $field ) {
382 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
389'entries' => $this->cache,
390'timestamps' => $this->timestamps,
391'maxCacheKeys' => $this->maxCacheKeys,
396 $this->cache = $data[
'entries'] ?? [];
397 $this->timestamps = $data[
'timestamps'] ?? [];
398// Fallback needed for serializations prior to T218511 399 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
408return $this->wallClockOverride ?: microtime(
true );
416 $this->wallClockOverride =& $time;
421class_alias( MapCacheLRU::class,
'MapCacheLRU' );
Store key-value entries in a size-limited in-memory LRU cache.
setMaxSize(int $maxKeys)
Resize the maximum number of cache entries, removing older entries as needed.
static newFromArray(array $values, $maxKeys)
makeKey(... $components)
Format a cache key string.
getWithSetCallback( $key, callable $callback, $rank=self::RANK_TOP, $maxAge=INF)
Get an item with the given key, producing and setting it if not found.
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
getMaxSize()
Get the maximum number of keys allowed.
setField( $key, $field, $value, $initRank=self::RANK_TOP)
has( $key, $maxAge=INF)
Check if a key exists.
hasField( $key, $field, $maxAge=INF)
__construct(int $maxKeys)
getField( $key, $field, $maxAge=INF)
Generic interface providing Time-To-Live constants for expirable object storage.