MediaWiki  1.34.0
MapCacheLRU Class Reference

Handles a simple LRU key/value map with a maximum number of entries. More...

Inheritance diagram for MapCacheLRU:
Collaboration diagram for MapCacheLRU:

Public Member Functions

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

Static Public Member Functions

static newFromArray (array $values, $maxKeys)
 

Protected Member Functions

 getCurrentTime ()
 

Private Member Functions

 getAge ( $key, $field=null)
 
 ping ( $key)
 Push an entry to the top of the cache. More...
 

Private Attributes

array $cache = []
 Map of (key => value) More...
 
float $epoch
 Default entry timestamp if not specified. More...
 
int $maxCacheKeys
 Max number of entries. More...
 
array $timestamps = []
 Map of (key => (UNIX timestamp, (field => UNIX timestamp))) More...
 
float null $wallClockOverride
 

Additional Inherited Members

- Public Attributes inherited from IExpiringStore
const ATTR_EMULATION = 1
 
const ATTR_SYNCWRITES = 2
 
const ERR_NO_RESPONSE = 1
 
const ERR_NONE = 0
 
const ERR_UNEXPECTED = 3
 
const ERR_UNREACHABLE = 2
 
const QOS_EMULATION_SQL = 1
 
const QOS_SYNCWRITES_BE = 2
 
const QOS_SYNCWRITES_NONE = 1
 
const QOS_SYNCWRITES_QC = 3
 
const QOS_SYNCWRITES_SS = 4
 
const QOS_UNKNOWN = INF
 
const TTL_DAY = 86400
 
const TTL_HOUR = 3600
 
const TTL_INDEFINITE = 0
 
const TTL_MINUTE = 60
 
const TTL_MONTH = 2592000
 
const TTL_PROC_LONG = 30
 
const TTL_PROC_SHORT = 3
 
const TTL_SECOND = 1
 
const TTL_WEEK = 604800
 
const TTL_YEAR = 31536000
 

Detailed Description

Handles a simple LRU key/value map with a maximum number of entries.

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.

See also
ProcessCacheLRU
Since
1.23

Definition at line 37 of file MapCacheLRU.php.

Constructor & Destructor Documentation

◆ __construct()

MapCacheLRU::__construct (   $maxKeys)
Parameters
int$maxKeysMaximum number of entries allowed (min 1)
Exceptions
ExceptionWhen $maxKeys is not an int or not above zero

Definition at line 63 of file MapCacheLRU.php.

References getCurrentTime().

Member Function Documentation

◆ clear()

MapCacheLRU::clear (   $keys = null)

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

Parameters
string | array | null$keys
Returns
void

Definition at line 285 of file MapCacheLRU.php.

References $keys.

Referenced by Title\clearCaches().

◆ get()

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$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

Definition at line 168 of file MapCacheLRU.php.

References has(), and ping().

Referenced by MessageCache\getAllMessageKeys(), MessageCache\load(), Title\newFromTextThrow(), JobQueueGroup\queuesHaveJobs(), and MessageCache\refreshAndReplaceInternal().

◆ getAge()

MapCacheLRU::getAge (   $key,
  $field = null 
)
private
Parameters
string | int$key
string | int | null$field[optional]
Returns
float UNIX timestamp; the age of the given entry or entry field

Definition at line 344 of file MapCacheLRU.php.

References $epoch, and getCurrentTime().

Referenced by has(), and hasField().

◆ getAllKeys()

MapCacheLRU::getAllKeys ( )
Returns
array
Since
1.25

Definition at line 247 of file MapCacheLRU.php.

◆ getCurrentTime()

MapCacheLRU::getCurrentTime ( )
protected
Returns
float UNIX timestamp

Definition at line 372 of file MapCacheLRU.php.

Referenced by __construct(), getAge(), set(), setField(), and unserialize().

◆ getField()

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 235 of file MapCacheLRU.php.

References hasField().

◆ getMaxSize()

MapCacheLRU::getMaxSize ( )

Get the maximum number of keys allowed.

Returns
int
Since
1.32

Definition at line 303 of file MapCacheLRU.php.

References $maxCacheKeys.

◆ getWithSetCallback()

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$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 264 of file MapCacheLRU.php.

References has().

Referenced by JobQueueGroup\getCachedConfigVar().

◆ has()

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

Check if a key exists.

Parameters
string$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.

References getAge().

Referenced by get(), getWithSetCallback(), set(), and setField().

◆ hasField()

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 213 of file MapCacheLRU.php.

References getAge().

Referenced by getField().

◆ newFromArray()

static 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 78 of file MapCacheLRU.php.

◆ ping()

MapCacheLRU::ping (   $key)
private

Push an entry to the top of the cache.

Parameters
string$key

Definition at line 333 of file MapCacheLRU.php.

Referenced by get(), set(), and setField().

◆ serialize()

MapCacheLRU::serialize ( )

Definition at line 354 of file MapCacheLRU.php.

◆ set()

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$key
mixed$value
float$rankBottom fraction of the list where keys start off [default: 1.0]
Returns
void

Definition at line 110 of file MapCacheLRU.php.

References getCurrentTime(), has(), and ping().

Referenced by MessageCache\load(), Title\newFromTextThrow(), and JobQueueGroup\push().

◆ setField()

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

Definition at line 184 of file MapCacheLRU.php.

References $type, getCurrentTime(), has(), and ping().

◆ setMaxSize()

MapCacheLRU::setMaxSize (   $maxKeys)

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

Parameters
int$maxKeysMaximum number of entries allowed (min 1)
Returns
void
Exceptions
ExceptionWhen $maxKeys is not an int or not above zero
Since
1.32

Definition at line 315 of file MapCacheLRU.php.

◆ setMockTime()

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

Definition at line 380 of file MapCacheLRU.php.

◆ toArray()

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

Definition at line 91 of file MapCacheLRU.php.

References $cache.

◆ unserialize()

MapCacheLRU::unserialize (   $serialized)

Definition at line 361 of file MapCacheLRU.php.

References $serialized, and getCurrentTime().

Member Data Documentation

◆ $cache

array MapCacheLRU::$cache = []
private

Map of (key => value)

Definition at line 39 of file MapCacheLRU.php.

Referenced by toArray().

◆ $epoch

float MapCacheLRU::$epoch
private

Default entry timestamp if not specified.

Definition at line 43 of file MapCacheLRU.php.

Referenced by getAge().

◆ $maxCacheKeys

int MapCacheLRU::$maxCacheKeys
private

Max number of entries.

Definition at line 46 of file MapCacheLRU.php.

Referenced by getMaxSize().

◆ $timestamps

array MapCacheLRU::$timestamps = []
private

Map of (key => (UNIX timestamp, (field => UNIX timestamp)))

Definition at line 41 of file MapCacheLRU.php.

◆ $wallClockOverride

float null MapCacheLRU::$wallClockOverride
private

Definition at line 49 of file MapCacheLRU.php.


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