40 private $timestamps = [];
45 private $maxCacheKeys;
48 private $wallClockOverride;
51 private const RANK_TOP = 1.0;
54 private const SIMPLE = 0;
56 private const FIELDS = 1;
62 if ( $maxKeys <= 0 ) {
63 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
66 $this->maxCacheKeys = $maxKeys;
78 $mapCache =
new self( $maxKeys );
79 $mapCache->cache = ( count( $values ) > $maxKeys )
80 ? array_slice( $values, -$maxKeys,
null,
true )
109 public function set( $key, $value, $rank = self::RANK_TOP ) {
110 if ( $this->
has( $key ) ) {
112 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
113 $evictKey = array_key_first( $this->cache );
114 unset( $this->cache[$evictKey] );
115 unset( $this->timestamps[$evictKey] );
118 if ( $rank < 1.0 && $rank > 0 ) {
119 $offset = intval( $rank * count( $this->cache ) );
120 $this->cache = array_slice( $this->cache, 0, $offset,
true )
122 + array_slice( $this->cache, $offset,
null,
true );
124 $this->cache[$key] = $value;
127 $this->timestamps[$key] = [
141 public function has( $key, $maxAge = INF ) {
142 if ( !is_int( $key ) && !is_string( $key ) ) {
143 throw new UnexpectedValueException(
144 __METHOD__ .
': invalid key; must be string or integer.' );
146 return array_key_exists( $key, $this->cache )
151 || $this->getKeyAge( $key ) <= $maxAge
171 public function get( $key, $maxAge = INF, $default = null ) {
172 if ( !$this->
has( $key, $maxAge ) ) {
178 return $this->cache[$key];
187 public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
188 if ( $this->
has( $key ) ) {
191 if ( !is_array( $this->cache[$key] ) ) {
192 $type = gettype( $this->cache[$key] );
193 throw new UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
196 $this->
set( $key, [], $initRank );
199 if ( !is_string( $field ) && !is_int( $field ) ) {
200 throw new UnexpectedValueException(
201 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
204 $this->cache[$key][$field] = $value;
205 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
215 public function hasField( $key, $field, $maxAge = INF ) {
216 $value = $this->
get( $key );
218 if ( !is_int( $field ) && !is_string( $field ) ) {
219 throw new UnexpectedValueException(
220 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
222 return is_array( $value )
223 && array_key_exists( $field, $value )
227 || $this->getKeyFieldAge( $key, $field ) <= $maxAge
238 public function getField( $key, $field, $maxAge = INF ) {
239 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
243 return $this->cache[$key][$field];
251 return array_keys( $this->cache );
268 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
270 if ( $this->
has( $key, $maxAge ) ) {
271 $value = $this->
get( $key );
273 $value = $callback();
274 if ( $value !==
false ) {
275 $this->
set( $key, $value, $rank );
289 if ( func_num_args() == 0 ) {
291 $this->timestamps = [];
293 foreach ( (array)
$keys as $key ) {
294 unset( $this->cache[$key] );
295 unset( $this->timestamps[$key] );
307 return $this->maxCacheKeys;
318 if ( $maxKeys <= 0 ) {
319 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
322 $this->maxCacheKeys = $maxKeys;
323 while ( count( $this->cache ) > $this->maxCacheKeys ) {
324 $evictKey = array_key_first( $this->cache );
325 unset( $this->cache[$evictKey] );
326 unset( $this->timestamps[$evictKey] );
335 private function ping( $key ) {
336 $item = $this->cache[$key];
337 unset( $this->cache[$key] );
338 $this->cache[$key] = $item;
345 private function getKeyAge( $key ) {
346 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
356 private function getKeyFieldAge( $key, $field ) {
357 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
364 'entries' => $this->cache,
365 'timestamps' => $this->timestamps,
366 'maxCacheKeys' => $this->maxCacheKeys,
371 $this->cache = $data[
'entries'] ?? [];
372 $this->timestamps = $data[
'timestamps'] ?? [];
374 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
383 return $this->wallClockOverride ?: microtime(
true );
391 $this->wallClockOverride =& $time;
Handles a simple LRU key/value map with a maximum number of entries.
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)
__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.