9use InvalidArgumentException;
10use UnexpectedValueException;
28 private $timestamps = [];
33 private $maxCacheKeys;
36 private $wallClockOverride;
39 private const RANK_TOP = 1.0;
42 private const SIMPLE = 0;
44 private const FIELDS = 1;
50 if ( $maxKeys <= 0 ) {
51 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
54 $this->maxCacheKeys = $maxKeys;
66 $mapCache =
new self( $maxKeys );
67 $mapCache->cache = ( count( $values ) > $maxKeys )
68 ? array_slice( $values, -$maxKeys,
null,
true )
97 public function set( $key, $value, $rank = self::RANK_TOP ) {
98 if ( $this->
has( $key ) ) {
100 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
101 $evictKey = array_key_first( $this->cache );
102 unset( $this->cache[$evictKey] );
103 unset( $this->timestamps[$evictKey] );
106 if ( $rank < 1.0 && $rank > 0 ) {
107 $offset = intval( $rank * count( $this->cache ) );
108 $this->cache = array_slice( $this->cache, 0, $offset,
true )
110 + array_slice( $this->cache, $offset,
null,
true );
112 $this->cache[$key] = $value;
115 $this->timestamps[$key] = [
129 public function has( $key, $maxAge = INF ) {
130 if ( !is_int( $key ) && !is_string( $key ) ) {
131 throw new UnexpectedValueException(
132 __METHOD__ .
': invalid key; must be string or integer.' );
134 return array_key_exists( $key, $this->cache )
139 || $this->getKeyAge( $key ) <= $maxAge
159 public function get( $key, $maxAge = INF, $default = null ) {
160 if ( !$this->
has( $key, $maxAge ) ) {
166 return $this->cache[$key];
175 public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) {
176 if ( $this->
has( $key ) ) {
179 if ( !is_array( $this->cache[$key] ) ) {
180 $type = get_debug_type( $this->cache[$key] );
181 throw new UnexpectedValueException(
"Cannot add field to non-array value ('$key' is $type)" );
184 $this->
set( $key, [], $initRank );
187 if ( !is_string( $field ) && !is_int( $field ) ) {
188 throw new UnexpectedValueException(
189 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
192 $this->cache[$key][$field] = $value;
193 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
203 public function hasField( $key, $field, $maxAge = INF ) {
204 $value = $this->
get( $key );
206 if ( !is_int( $field ) && !is_string( $field ) ) {
207 throw new UnexpectedValueException(
208 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
210 return is_array( $value )
211 && array_key_exists( $field, $value )
215 || $this->getKeyFieldAge( $key, $field ) <= $maxAge
226 public function getField( $key, $field, $maxAge = INF ) {
227 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
231 return $this->cache[$key][$field];
239 return array_keys( $this->cache );
256 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF
258 if ( $this->
has( $key, $maxAge ) ) {
259 $value = $this->
get( $key );
261 $value = $callback();
262 if ( $value !==
false ) {
263 $this->
set( $key, $value, $rank );
284 foreach ( $components as $i => $component ) {
288 $key .= strtr( $component, [
'%' =>
'%25',
':' =>
'%3A' ] );
299 public function clear( $keys =
null ) {
300 if ( func_num_args() == 0 ) {
302 $this->timestamps = [];
304 foreach ( (array)$keys as $key ) {
305 unset( $this->cache[$key] );
306 unset( $this->timestamps[$key] );
318 return $this->maxCacheKeys;
329 if ( $maxKeys <= 0 ) {
330 throw new InvalidArgumentException(
'$maxKeys must be above zero' );
333 $this->maxCacheKeys = $maxKeys;
334 while ( count( $this->cache ) > $this->maxCacheKeys ) {
335 $evictKey = array_key_first( $this->cache );
336 unset( $this->cache[$evictKey] );
337 unset( $this->timestamps[$evictKey] );
346 private function ping( $key ) {
347 $item = $this->cache[$key];
348 unset( $this->cache[$key] );
349 $this->cache[$key] = $item;
356 private function getKeyAge( $key ) {
357 $mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch;
367 private function getKeyFieldAge( $key, $field ) {
368 $mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch;
375 'entries' => $this->cache,
376 'timestamps' => $this->timestamps,
377 'maxCacheKeys' => $this->maxCacheKeys,
382 $this->cache = $data[
'entries'] ?? [];
383 $this->timestamps = $data[
'timestamps'] ?? [];
385 $this->maxCacheKeys = $data[
'maxCacheKeys'] ?? ( count( $this->cache ) + 1 );
394 return $this->wallClockOverride ?: microtime(
true );
402 $this->wallClockOverride =& $time;
407class_alias( MapCacheLRU::class,
'MapCacheLRU' );