38 private $timestamps = [];
43 private $maxCacheKeys;
46 private $wallClockOverride;
49 private const RANK_TOP = 1.0;
52 private const SIMPLE = 0;
54 private const FIELDS = 1;
60 if ( $maxKeys <= 0 ) {
61 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
64 $this->maxCacheKeys = $maxKeys;
76 $mapCache =
new self( $maxKeys );
77 $mapCache->cache = ( count( $values ) > $maxKeys )
78 ? array_slice( $values, -$maxKeys,
null,
true )
107 public function set( $key, $value, $rank = self::RANK_TOP ) {
108 if ( $this->
has( $key ) ) {
110 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
111 $evictKey = array_key_first( $this->cache );
112 unset( $this->cache[$evictKey] );
113 unset( $this->timestamps[$evictKey] );
116 if ( $rank < 1.0 && $rank > 0 ) {
117 $offset = intval( $rank * count( $this->cache ) );
118 $this->cache = array_slice( $this->cache, 0, $offset,
true )
120 + array_slice( $this->cache, $offset,
null,
true );
122 $this->cache[$key] = $value;
125 $this->timestamps[$key] = [
139 public function has( $key, $maxAge = INF ) {
140 if ( !is_int( $key ) && !is_string( $key ) ) {
141 throw new UnexpectedValueException(
142 __METHOD__ .
': invalid key; must be string or integer.' );
144 return array_key_exists( $key, $this->cache )
149 || $this->getKeyAge( $key ) <= $maxAge
169 public function get( $key, $maxAge = INF, $default = null ) {
170 if ( !$this->
has( $key, $maxAge ) ) {
176 return $this->cache[$key];
185 public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
186 if ( $this->
has( $key ) ) {
189 if ( !is_array( $this->cache[$key] ) ) {
190 $type = get_debug_type( $this->cache[$key] );
191 throw new UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
194 $this->
set( $key, [], $initRank );
197 if ( !is_string( $field ) && !is_int( $field ) ) {
198 throw new UnexpectedValueException(
199 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
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." );
220 return is_array( $value )
221 && array_key_exists( $field, $value )
225 || $this->getKeyFieldAge( $key, $field ) <= $maxAge
236 public function getField( $key, $field, $maxAge = INF ) {
237 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
241 return $this->cache[$key][$field];
249 return array_keys( $this->cache );
266 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
268 if ( $this->
has( $key, $maxAge ) ) {
269 $value = $this->
get( $key );
271 $value = $callback();
272 if ( $value !==
false ) {
273 $this->
set( $key, $value, $rank );
294 foreach ( $components as $i => $component ) {
298 $key .= strtr( $component, [
'%' =>
'%25',
':' =>
'%3A' ] );
309 public function clear( $keys =
null ) {
310 if ( func_num_args() == 0 ) {
312 $this->timestamps = [];
314 foreach ( (array)$keys as $key ) {
315 unset( $this->cache[$key] );
316 unset( $this->timestamps[$key] );
328 return $this->maxCacheKeys;
339 if ( $maxKeys <= 0 ) {
340 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
343 $this->maxCacheKeys = $maxKeys;
344 while ( count( $this->cache ) > $this->maxCacheKeys ) {
345 $evictKey = array_key_first( $this->cache );
346 unset( $this->cache[$evictKey] );
347 unset( $this->timestamps[$evictKey] );
356 private function ping( $key ) {
357 $item = $this->cache[$key];
358 unset( $this->cache[$key] );
359 $this->cache[$key] = $item;
366 private function getKeyAge( $key ) {
367 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
377 private function getKeyFieldAge( $key, $field ) {
378 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
385 'entries' => $this->cache,
386 'timestamps' => $this->timestamps,
387 'maxCacheKeys' => $this->maxCacheKeys,
392 $this->cache = $data[
'entries'] ?? [];
393 $this->timestamps = $data[
'timestamps'] ?? [];
395 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
404 return $this->wallClockOverride ?: microtime(
true );
412 $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.