37 private $timestamps = [];
42 private $maxCacheKeys;
45 private $wallClockOverride;
48 private const RANK_TOP = 1.0;
51 private const SIMPLE = 0;
53 private const FIELDS = 1;
59 if ( $maxKeys <= 0 ) {
60 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
63 $this->maxCacheKeys = $maxKeys;
75 $mapCache =
new self( $maxKeys );
76 $mapCache->cache = ( count( $values ) > $maxKeys )
77 ? array_slice( $values, -$maxKeys,
null,
true )
106 public function set( $key, $value, $rank = self::RANK_TOP ) {
107 if ( $this->
has( $key ) ) {
109 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
110 $evictKey = array_key_first( $this->cache );
111 unset( $this->cache[$evictKey] );
112 unset( $this->timestamps[$evictKey] );
115 if ( $rank < 1.0 && $rank > 0 ) {
116 $offset = intval( $rank * count( $this->cache ) );
117 $this->cache = array_slice( $this->cache, 0, $offset,
true )
119 + array_slice( $this->cache, $offset,
null,
true );
121 $this->cache[$key] = $value;
124 $this->timestamps[$key] = [
138 public function has( $key, $maxAge = INF ) {
139 if ( !is_int( $key ) && !is_string( $key ) ) {
140 throw new UnexpectedValueException(
141 __METHOD__ .
': invalid key; must be string or integer.' );
143 return array_key_exists( $key, $this->cache )
148 || $this->getKeyAge( $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 if ( !is_array( $this->cache[$key] ) ) {
189 $type = get_debug_type( $this->cache[$key] );
190 throw new UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
193 $this->
set( $key, [], $initRank );
196 if ( !is_string( $field ) && !is_int( $field ) ) {
197 throw new UnexpectedValueException(
198 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
201 $this->cache[$key][$field] = $value;
202 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
212 public function hasField( $key, $field, $maxAge = INF ) {
213 $value = $this->
get( $key );
215 if ( !is_int( $field ) && !is_string( $field ) ) {
216 throw new UnexpectedValueException(
217 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
219 return is_array( $value )
220 && array_key_exists( $field, $value )
224 || $this->getKeyFieldAge( $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 = $callback();
271 if ( $value !==
false ) {
272 $this->
set( $key, $value, $rank );
293 foreach ( $components as $i => $component ) {
297 $key .= strtr( $component, [
'%' =>
'%25',
':' =>
'%3A' ] );
308 public function clear( $keys =
null ) {
309 if ( func_num_args() == 0 ) {
311 $this->timestamps = [];
313 foreach ( (array)$keys as $key ) {
314 unset( $this->cache[$key] );
315 unset( $this->timestamps[$key] );
327 return $this->maxCacheKeys;
338 if ( $maxKeys <= 0 ) {
339 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
342 $this->maxCacheKeys = $maxKeys;
343 while ( count( $this->cache ) > $this->maxCacheKeys ) {
344 $evictKey = array_key_first( $this->cache );
345 unset( $this->cache[$evictKey] );
346 unset( $this->timestamps[$evictKey] );
355 private function ping( $key ) {
356 $item = $this->cache[$key];
357 unset( $this->cache[$key] );
358 $this->cache[$key] = $item;
365 private function getKeyAge( $key ) {
366 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
376 private function getKeyFieldAge( $key, $field ) {
377 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
384 'entries' => $this->cache,
385 'timestamps' => $this->timestamps,
386 'maxCacheKeys' => $this->maxCacheKeys,
391 $this->cache = $data[
'entries'] ?? [];
392 $this->timestamps = $data[
'timestamps'] ?? [];
394 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
403 return $this->wallClockOverride ?: microtime(
true );
411 $this->wallClockOverride =& $time;
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.
has( $key, $maxAge=INF)
Check if a key exists.
static newFromArray(array $values, $maxKeys)
getField( $key, $field, $maxAge=INF)
hasField( $key, $field, $maxAge=INF)
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)
makeKey(... $components)
Format a cache key string.
__construct(int $maxKeys)
getWithSetCallback( $key, callable $callback, $rank=self::RANK_TOP, $maxAge=INF)
Get an item with the given key, producing and setting it if not found.