23use Wikimedia\Assert\Assert;
64 Assert::parameterType(
'integer', $maxKeys,
'$maxKeys' );
65 Assert::parameter( $maxKeys > 0,
'$maxKeys',
'must be above zero' );
67 $this->maxCacheKeys = $maxKeys;
79 $mapCache =
new self( $maxKeys );
80 $mapCache->cache = ( count( $values ) > $maxKeys )
81 ? array_slice( $values, -$maxKeys,
null,
true )
110 public function set( $key, $value, $rank = self::RANK_TOP ) {
111 if ( $this->
has( $key ) ) {
113 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
114 reset( $this->cache );
115 $evictKey = key( $this->cache );
116 unset( $this->cache[$evictKey] );
117 unset( $this->timestamps[$evictKey] );
120 if ( $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] = [
143 public function has( $key, $maxAge = INF ) {
144 if ( !is_int( $key ) && !is_string( $key ) ) {
145 throw new UnexpectedValueException(
146 __METHOD__ .
': invalid key; must be string or integer.' );
149 if ( !array_key_exists( $key, $this->cache ) ) {
153 return ( $maxAge <= 0 || $this->
getAge( $key ) <= $maxAge );
168 public function get( $key, $maxAge = INF, $default = null ) {
169 if ( !$this->
has( $key, $maxAge ) ) {
175 return $this->cache[$key];
184 public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
185 if ( $this->
has( $key ) ) {
188 $this->
set( $key, [], $initRank );
191 if ( !is_int( $field ) && !is_string( $field ) ) {
192 throw new UnexpectedValueException(
193 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
196 if ( !is_array( $this->cache[$key] ) ) {
197 $type = gettype( $this->cache[$key] );
199 throw new UnexpectedValueException(
"The value of '$key' ($type) is not an array." );
202 $this->cache[$key][$field] = $value;
203 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
213 public function hasField( $key, $field, $maxAge = INF ) {
214 $value = $this->
get( $key );
216 if ( !is_int( $field ) && !is_string( $field ) ) {
217 throw new UnexpectedValueException(
218 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
221 if ( !is_array( $value ) || !array_key_exists( $field, $value ) ) {
225 return ( $maxAge <= 0 || $this->
getAge( $key, $field ) <= $maxAge );
235 public function getField( $key, $field, $maxAge = INF ) {
236 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
240 return $this->cache[$key][$field];
248 return array_keys( $this->cache );
265 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
267 if ( $this->
has( $key, $maxAge ) ) {
268 $value = $this->
get( $key );
270 $value = call_user_func( $callback );
271 if ( $value !==
false ) {
272 $this->
set( $key, $value, $rank );
286 if ( func_num_args() == 0 ) {
288 $this->timestamps = [];
290 foreach ( (array)
$keys as $key ) {
291 unset( $this->cache[$key] );
292 unset( $this->timestamps[$key] );
304 return $this->maxCacheKeys;
316 Assert::parameterType(
'integer', $maxKeys,
'$maxKeys' );
317 Assert::parameter( $maxKeys > 0,
'$maxKeys',
'must be above zero' );
319 $this->maxCacheKeys = $maxKeys;
320 while ( count( $this->cache ) > $this->maxCacheKeys ) {
321 reset( $this->cache );
322 $evictKey = key( $this->cache );
323 unset( $this->cache[$evictKey] );
324 unset( $this->timestamps[$evictKey] );
333 private function ping( $key ) {
334 $item = $this->cache[$key];
335 unset( $this->cache[$key] );
336 $this->cache[$key] = $item;
344 private function getAge( $key, $field =
null ) {
345 if ( $field !==
null ) {
346 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
348 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
356 'entries' => $this->cache,
357 'timestamps' => $this->timestamps
363 $this->cache = $data[
'entries'] ?? [];
364 $this->timestamps = $data[
'timestamps'] ?? [];
373 return $this->wallClockOverride ?: microtime(
true );
381 $this->wallClockOverride =& $time;
unserialize( $serialized)
Handles a simple LRU key/value map with a maximum number of entries.
getAge( $key, $field=null)
int $maxCacheKeys
Max number of entries.
has( $key, $maxAge=INF)
Check if a key exists.
static newFromArray(array $values, $maxKeys)
getField( $key, $field, $maxAge=INF)
ping( $key)
Push an entry to the top of the cache.
float $epoch
Default entry timestamp if not specified.
hasField( $key, $field, $maxAge=INF)
unserialize( $serialized)
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
setMaxSize( $maxKeys)
Resize the maximum number of cache entries, removing older entries as needed.
getMaxSize()
Get the maximum number of keys allowed.
setField( $key, $field, $value, $initRank=self::RANK_TOP)
getWithSetCallback( $key, callable $callback, $rank=self::RANK_TOP, $maxAge=INF)
Get an item with the given key, producing and setting it if not found.
array $timestamps
Map of (key => (UNIX timestamp, (field => UNIX timestamp)))
float null $wallClockOverride
Generic interface for lightweight expiring object stores.
foreach( $res as $row) $serialized