23use InvalidArgumentException;
24use UnexpectedValueException;
42 private $timestamps = [];
47 private $maxCacheKeys;
50 private $wallClockOverride;
53 private const RANK_TOP = 1.0;
56 private const SIMPLE = 0;
58 private const FIELDS = 1;
64 if ( $maxKeys <= 0 ) {
65 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
68 $this->maxCacheKeys = $maxKeys;
80 $mapCache =
new self( $maxKeys );
81 $mapCache->cache = ( count( $values ) > $maxKeys )
82 ? array_slice( $values, -$maxKeys,
null,
true )
111 public function set( $key, $value, $rank = self::RANK_TOP ) {
112 if ( $this->
has( $key ) ) {
114 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
115 $evictKey = array_key_first( $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.' );
148 return array_key_exists( $key, $this->cache )
153 || $this->getKeyAge( $key ) <= $maxAge
173 public function get( $key, $maxAge = INF, $default = null ) {
174 if ( !$this->
has( $key, $maxAge ) ) {
180 return $this->cache[$key];
189 public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
190 if ( $this->
has( $key ) ) {
193 if ( !is_array( $this->cache[$key] ) ) {
194 $type = get_debug_type( $this->cache[$key] );
195 throw new UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
198 $this->
set( $key, [], $initRank );
201 if ( !is_string( $field ) && !is_int( $field ) ) {
202 throw new UnexpectedValueException(
203 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
206 $this->cache[$key][$field] = $value;
207 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
217 public function hasField( $key, $field, $maxAge = INF ) {
218 $value = $this->
get( $key );
220 if ( !is_int( $field ) && !is_string( $field ) ) {
221 throw new UnexpectedValueException(
222 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
224 return is_array( $value )
225 && array_key_exists( $field, $value )
229 || $this->getKeyFieldAge( $key, $field ) <= $maxAge
240 public function getField( $key, $field, $maxAge = INF ) {
241 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
245 return $this->cache[$key][$field];
253 return array_keys( $this->cache );
270 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
272 if ( $this->
has( $key, $maxAge ) ) {
273 $value = $this->
get( $key );
275 $value = $callback();
276 if ( $value !==
false ) {
277 $this->
set( $key, $value, $rank );
298 foreach ( $components as $i => $component ) {
302 $key .= strtr( $component, [
'%' =>
'%25',
':' =>
'%3A' ] );
313 public function clear( $keys =
null ) {
314 if ( func_num_args() == 0 ) {
316 $this->timestamps = [];
318 foreach ( (array)$keys as $key ) {
319 unset( $this->cache[$key] );
320 unset( $this->timestamps[$key] );
332 return $this->maxCacheKeys;
343 if ( $maxKeys <= 0 ) {
344 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
347 $this->maxCacheKeys = $maxKeys;
348 while ( count( $this->cache ) > $this->maxCacheKeys ) {
349 $evictKey = array_key_first( $this->cache );
350 unset( $this->cache[$evictKey] );
351 unset( $this->timestamps[$evictKey] );
360 private function ping( $key ) {
361 $item = $this->cache[$key];
362 unset( $this->cache[$key] );
363 $this->cache[$key] = $item;
370 private function getKeyAge( $key ) {
371 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
381 private function getKeyFieldAge( $key, $field ) {
382 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
389 'entries' => $this->cache,
390 'timestamps' => $this->timestamps,
391 'maxCacheKeys' => $this->maxCacheKeys,
396 $this->cache = $data[
'entries'] ?? [];
397 $this->timestamps = $data[
'timestamps'] ?? [];
399 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
408 return $this->wallClockOverride ?: microtime(
true );
416 $this->wallClockOverride =& $time;
421class_alias( MapCacheLRU::class,
'MapCacheLRU' );