MediaWiki REL1_35
|
Handles a simple LRU key/value map with a maximum number of entries. More...
Public Member Functions | |
__construct ( $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) | |
serialize () | |
set ( $key, $value, $rank=self::RANK_TOP) | |
Set a key/value pair. | |
setField ( $key, $field, $value, $initRank=self::RANK_TOP) | |
setMaxSize ( $maxKeys) | |
Resize the maximum number of cache entries, removing older entries as needed. | |
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. | |
Private Attributes | |
array | $cache = [] |
Map of (key => value) | |
float | $epoch |
Default entry timestamp if not specified. | |
int | $maxCacheKeys |
Max number of entries. | |
array | $timestamps = [] |
Map of (key => (UNIX timestamp, (field => UNIX timestamp))) | |
float null | $wallClockOverride |
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.
Definition at line 38 of file MapCacheLRU.php.
MapCacheLRU::__construct | ( | $maxKeys | ) |
int | $maxKeys | Maximum number of entries allowed (min 1) |
Exception | When $maxKeys is not an int or not above zero |
Definition at line 64 of file MapCacheLRU.php.
References getCurrentTime().
MapCacheLRU::__serialize | ( | ) |
Definition at line 355 of file MapCacheLRU.php.
References $cache.
MapCacheLRU::__unserialize | ( | $data | ) |
Definition at line 367 of file MapCacheLRU.php.
MapCacheLRU::clear | ( | $keys = null | ) |
Clear one or several cache entries, or all cache entries.
string | array | null | $keys |
Definition at line 286 of file MapCacheLRU.php.
References $keys.
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.
string | $key | |
float | $maxAge | Ignore items older than this many seconds [default: INF] |
mixed | null | $default | Value to return if no key is found [default: null] |
Definition at line 169 of file MapCacheLRU.php.
|
private |
string | int | $key | |
string | int | null | $field | [optional] |
Definition at line 345 of file MapCacheLRU.php.
References getCurrentTime().
Referenced by has(), and hasField().
MapCacheLRU::getAllKeys | ( | ) |
|
protected |
Definition at line 383 of file MapCacheLRU.php.
Referenced by __construct(), getAge(), set(), and setField().
MapCacheLRU::getField | ( | $key, | |
$field, | |||
$maxAge = INF |
|||
) |
string | int | $key | |
string | int | $field | |
float | $maxAge | Ignore items older than this many seconds [default: INF] |
Definition at line 236 of file MapCacheLRU.php.
References hasField().
MapCacheLRU::getMaxSize | ( | ) |
Get the maximum number of keys allowed.
Definition at line 304 of file MapCacheLRU.php.
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.
string | $key | |
callable | $callback | Callback that will produce the value |
float | $rank | Bottom fraction of the list where keys start off [default: 1.0] |
float | $maxAge | Ignore items older than this many seconds [default: INF] |
Definition at line 265 of file MapCacheLRU.php.
References has().
MapCacheLRU::has | ( | $key, | |
$maxAge = INF |
|||
) |
Check if a key exists.
string | $key | |
float | $maxAge | Ignore items older than this many seconds [default: INF] |
Definition at line 144 of file MapCacheLRU.php.
References getAge().
Referenced by get(), getWithSetCallback(), set(), and setField().
MapCacheLRU::hasField | ( | $key, | |
$field, | |||
$maxAge = INF |
|||
) |
string | int | $key | |
string | int | $field | |
float | $maxAge | Ignore items older than this many seconds [default: INF] |
Definition at line 214 of file MapCacheLRU.php.
References getAge().
Referenced by getField().
|
static |
array | $values | Key/value map in order of increasingly recent access |
int | $maxKeys |
Definition at line 79 of file MapCacheLRU.php.
|
private |
Push an entry to the top of the cache.
string | $key |
Definition at line 334 of file MapCacheLRU.php.
Referenced by get(), set(), and setField().
MapCacheLRU::serialize | ( | ) |
Definition at line 363 of file MapCacheLRU.php.
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.
string | $key | |
mixed | $value | |
float | $rank | Bottom fraction of the list where keys start off [default: 1.0] |
Definition at line 111 of file MapCacheLRU.php.
References getCurrentTime(), has(), and ping().
MapCacheLRU::setField | ( | $key, | |
$field, | |||
$value, | |||
$initRank = self::RANK_TOP |
|||
) |
string | int | $key | |
string | int | $field | |
mixed | $value | |
float | $initRank |
Definition at line 185 of file MapCacheLRU.php.
References $type, getCurrentTime(), has(), and ping().
MapCacheLRU::setMaxSize | ( | $maxKeys | ) |
Resize the maximum number of cache entries, removing older entries as needed.
int | $maxKeys | Maximum number of entries allowed (min 1) |
Exception | When $maxKeys is not an int or not above zero |
Definition at line 316 of file MapCacheLRU.php.
MapCacheLRU::setMockTime | ( | & | $time | ) |
float | null | &$time | Mock UNIX timestamp for testing |
Definition at line 391 of file MapCacheLRU.php.
MapCacheLRU::toArray | ( | ) |
Definition at line 92 of file MapCacheLRU.php.
References $cache.
MapCacheLRU::unserialize | ( | $serialized | ) |
Definition at line 375 of file MapCacheLRU.php.
|
private |
Map of (key => value)
Definition at line 40 of file MapCacheLRU.php.
|
private |
Default entry timestamp if not specified.
Definition at line 44 of file MapCacheLRU.php.
|
private |
Max number of entries.
Definition at line 47 of file MapCacheLRU.php.
|
private |
Map of (key => (UNIX timestamp, (field => UNIX timestamp)))
Definition at line 42 of file MapCacheLRU.php.
|
private |
Definition at line 50 of file MapCacheLRU.php.