MediaWiki master
Wikimedia\MapCacheLRU\MapCacheLRU Class Reference

Store key-value entries in a size-limited in-memory LRU cache. More...

Inherits Wikimedia\LightweightObjectStore\ExpirationAwareness.

Collaboration diagram for Wikimedia\MapCacheLRU\MapCacheLRU:

Public Member Functions

 __construct (int $maxKeys)
 
 __serialize ()
 
 __unserialize ( $data)
 
 clear ( $keys=null)
 Clear one or several cache entries, or all cache entries.
 
 get ( $key, $maxAge=INF, $default=null)
 Get the value for a key.
 
 getAllKeys ()
 
 getField ( $key, $field, $maxAge=INF)
 
 getMaxSize ()
 Get the maximum number of keys allowed.
 
 getWithSetCallback ( $key, callable $callback, $rank=self::RANK_TOP, $maxAge=INF)
 Get an item with the given key, producing and setting it if not found.
 
 has ( $key, $maxAge=INF)
 Check if a key exists.
 
 hasField ( $key, $field, $maxAge=INF)
 
 makeKey (... $components)
 Format a cache key string.
 
 set ( $key, $value, $rank=self::RANK_TOP)
 Set a key/value pair.
 
 setField ( $key, $field, $value, $initRank=self::RANK_TOP)
 
 setMaxSize (int $maxKeys)
 Resize the maximum number of cache entries, removing older entries as needed.
 
 setMockTime (&$time)
 
 toArray ()
 

Static Public Member Functions

static newFromArray (array $values, $maxKeys)
 

Protected Member Functions

 getCurrentTime ()
 

Detailed Description

Store key-value entries in a size-limited in-memory LRU cache.

The last-modification timestamp of entries is internally tracked so that callers can specify the maximum acceptable age of entries in calls to the has() method. As a convenience, the hasField(), getField(), and setField() methods can be used for entries that are field/value maps themselves; such fields will have their own internally tracked last-modification timestamp.

Since
1.23

Definition at line 38 of file MapCacheLRU.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\MapCacheLRU\MapCacheLRU::__construct ( int $maxKeys)
Parameters
int$maxKeysMaximum number of entries allowed (min 1)

Definition at line 63 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\getCurrentTime().

Member Function Documentation

◆ __serialize()

Wikimedia\MapCacheLRU\MapCacheLRU::__serialize ( )

Definition at line 387 of file MapCacheLRU.php.

◆ __unserialize()

Wikimedia\MapCacheLRU\MapCacheLRU::__unserialize ( $data)

◆ clear()

Wikimedia\MapCacheLRU\MapCacheLRU::clear ( $keys = null)

Clear one or several cache entries, or all cache entries.

Parameters
string | int | array | null$keys
Returns
void

Definition at line 313 of file MapCacheLRU.php.

◆ get()

Wikimedia\MapCacheLRU\MapCacheLRU::get ( $key,
$maxAge = INF,
$default = null )

Get the value for a key.

This returns null if the key is not set. If the item is already set, it will be pushed to the top of the cache.

Parameters
string | int$key
float$maxAgeIgnore items older than this many seconds [default: INF]
mixed | null$defaultValue to return if no key is found [default: null]
Returns
mixed Returns $default if the key was not found or is older than $maxAge
Since
1.32 Added $maxAge
1.34 Added $default

Although sometimes this can be tainted, taint-check doesn't distinguish separate instances of MapCacheLRU, so assume untainted to cut down on false positives. See T272134.

Definition at line 173 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\has().

Referenced by MediaWiki\JobQueue\JobQueueGroup\queuesHaveJobs().

◆ getAllKeys()

Wikimedia\MapCacheLRU\MapCacheLRU::getAllKeys ( )
Returns
array
Since
1.25

Definition at line 252 of file MapCacheLRU.php.

◆ getCurrentTime()

Wikimedia\MapCacheLRU\MapCacheLRU::getCurrentTime ( )
protected

◆ getField()

Wikimedia\MapCacheLRU\MapCacheLRU::getField ( $key,
$field,
$maxAge = INF )
Parameters
string | int$key
string | int$field
float$maxAgeIgnore items older than this many seconds [default: INF]
Returns
mixed Returns null if the key was not found or is older than $maxAge
Since
1.32 Added $maxAge

Definition at line 240 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\hasField().

◆ getMaxSize()

Wikimedia\MapCacheLRU\MapCacheLRU::getMaxSize ( )

Get the maximum number of keys allowed.

Returns
int
Since
1.32

Definition at line 331 of file MapCacheLRU.php.

◆ getWithSetCallback()

Wikimedia\MapCacheLRU\MapCacheLRU::getWithSetCallback ( $key,
callable $callback,
$rank = self::RANK_TOP,
$maxAge = INF )

Get an item with the given key, producing and setting it if not found.

If the callback returns false, then nothing is stored.

Since
1.28
Parameters
string | int$key
callable$callbackCallback that will produce the value
float$rankBottom fraction of the list where keys start off [default: 1.0]
float$maxAgeIgnore items older than this many seconds [default: INF]
Returns
mixed The cached value if found or the result of $callback otherwise
Since
1.32 Added $maxAge

Definition at line 269 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\has().

◆ has()

Wikimedia\MapCacheLRU\MapCacheLRU::has ( $key,
$maxAge = INF )

Check if a key exists.

Parameters
string | int$key
float$maxAgeIgnore items older than this many seconds [default: INF]
Returns
bool
Since
1.32 Added $maxAge

Definition at line 143 of file MapCacheLRU.php.

Referenced by Wikimedia\MapCacheLRU\MapCacheLRU\get(), Wikimedia\MapCacheLRU\MapCacheLRU\getWithSetCallback(), Wikimedia\MapCacheLRU\MapCacheLRU\set(), and Wikimedia\MapCacheLRU\MapCacheLRU\setField().

◆ hasField()

Wikimedia\MapCacheLRU\MapCacheLRU::hasField ( $key,
$field,
$maxAge = INF )
Parameters
string | int$key
string | int$field
float$maxAgeIgnore items older than this many seconds [default: INF]
Returns
bool
Since
1.32 Added $maxAge

Definition at line 217 of file MapCacheLRU.php.

Referenced by Wikimedia\MapCacheLRU\MapCacheLRU\getField().

◆ makeKey()

Wikimedia\MapCacheLRU\MapCacheLRU::makeKey ( $components)

Format a cache key string.

Since
1.41
Parameters
string|int...$components Key components
Returns
string

Definition at line 291 of file MapCacheLRU.php.

◆ newFromArray()

static Wikimedia\MapCacheLRU\MapCacheLRU::newFromArray ( array $values,
$maxKeys )
static
Parameters
array$valuesKey/value map in order of increasingly recent access
int$maxKeys
Returns
MapCacheLRU
Since
1.30

Definition at line 79 of file MapCacheLRU.php.

◆ set()

Wikimedia\MapCacheLRU\MapCacheLRU::set ( $key,
$value,
$rank = self::RANK_TOP )

Set a key/value pair.

This will prune the cache if it gets too large based on LRU. If the item is already set, it will be pushed to the top of the cache.

To reduce evictions due to one-off use of many new keys, $rank can be set to have keys start at the top of a bottom fraction of the list. A value of 3/8 means values start at the top of the bottom 3/8s of the list and are moved to the top of the list when accessed a second time.

Parameters
string | int$key
mixed$value
float$rankBottom fraction of the list where keys start off [default: 1.0]
Returns
void

Definition at line 111 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\getCurrentTime(), and Wikimedia\MapCacheLRU\MapCacheLRU\has().

Referenced by MediaWiki\JobQueue\JobQueueGroup\push().

◆ setField()

Wikimedia\MapCacheLRU\MapCacheLRU::setField ( $key,
$field,
$value,
$initRank = self::RANK_TOP )
Parameters
string | int$key
string | int$field
mixed$value
float$initRank

Definition at line 189 of file MapCacheLRU.php.

References Wikimedia\MapCacheLRU\MapCacheLRU\getCurrentTime(), and Wikimedia\MapCacheLRU\MapCacheLRU\has().

◆ setMaxSize()

Wikimedia\MapCacheLRU\MapCacheLRU::setMaxSize ( int $maxKeys)

Resize the maximum number of cache entries, removing older entries as needed.

Parameters
int$maxKeysMaximum number of entries allowed (min 1)
Returns
void
Since
1.32

Definition at line 342 of file MapCacheLRU.php.

◆ setMockTime()

Wikimedia\MapCacheLRU\MapCacheLRU::setMockTime ( & $time)
Parameters
float | null&$timeMock UNIX timestamp for testing

Definition at line 415 of file MapCacheLRU.php.

◆ toArray()

Wikimedia\MapCacheLRU\MapCacheLRU::toArray ( )
Returns
array Key/value map in order of increasingly recent access
Since
1.30

Definition at line 92 of file MapCacheLRU.php.


The documentation for this class was generated from the following file: