23 use Wikimedia\Assert\Assert;
63 Assert::parameterType(
'integer', $maxKeys,
'$maxKeys' );
64 Assert::parameter( $maxKeys > 0,
'$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 )
110 if ( $this->
has( $key ) ) {
112 } elseif (
count( $this->
cache ) >= $this->maxCacheKeys ) {
113 reset( $this->
cache );
115 unset( $this->
cache[$evictKey] );
116 unset( $this->timestamps[$evictKey] );
119 if ( $rank < 1.0 && $rank > 0 ) {
120 $offset = intval( $rank *
count( $this->
cache ) );
121 $this->
cache = array_slice( $this->
cache, 0, $offset,
true )
123 + array_slice( $this->
cache, $offset,
null,
true );
128 $this->timestamps[$key] = [
141 public function has( $key, $maxAge = 0.0 ) {
142 if ( !is_int( $key ) && !is_string( $key ) ) {
143 throw new UnexpectedValueException(
144 __METHOD__ .
': invalid key; must be string or integer.' );
147 if ( !array_key_exists( $key, $this->
cache ) ) {
151 return ( $maxAge <= 0 || $this->
getAge( $key ) <= $maxAge );
163 public function get( $key, $maxAge = 0.0 ) {
164 if ( !$this->
has( $key, $maxAge ) ) {
170 return $this->
cache[$key];
180 if ( $this->
has( $key ) ) {
183 $this->
set( $key, [], $initRank );
186 if ( !is_int( $field ) && !is_string( $field ) ) {
187 throw new UnexpectedValueException(
188 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
191 if ( !is_array( $this->
cache[$key] ) ) {
194 throw new UnexpectedValueException(
"The value of '$key' ($type) is not an array." );
198 $this->timestamps[$key][self::FIELDS][$field] = $this->
getCurrentTime();
207 public function hasField( $key, $field, $maxAge = 0.0 ) {
208 $value = $this->
get( $key );
210 if ( !is_int( $field ) && !is_string( $field ) ) {
211 throw new UnexpectedValueException(
212 __METHOD__ .
": invalid field for '$key'; must be string or integer." );
215 if ( !is_array(
$value ) || !array_key_exists( $field,
$value ) ) {
219 return ( $maxAge <= 0 || $this->
getAge( $key, $field ) <= $maxAge );
228 public function getField( $key, $field, $maxAge = 0.0 ) {
229 if ( !$this->
hasField( $key, $field, $maxAge ) ) {
233 return $this->
cache[$key][$field];
241 return array_keys( $this->
cache );
257 $key, callable $callback, $rank = self::RANK_TOP, $maxAge = 0.0
259 if ( $this->
has( $key, $maxAge ) ) {
260 $value = $this->
get( $key );
262 $value = call_user_func( $callback );
264 $this->
set( $key,
$value, $rank );
278 if ( func_num_args() == 0 ) {
280 $this->timestamps = [];
283 unset( $this->
cache[$key] );
284 unset( $this->timestamps[$key] );
308 Assert::parameterType(
'integer', $maxKeys,
'$maxKeys' );
309 Assert::parameter( $maxKeys > 0,
'$maxKeys',
'must be above zero' );
311 $this->maxCacheKeys = $maxKeys;
312 while (
count( $this->
cache ) > $this->maxCacheKeys ) {
313 reset( $this->
cache );
315 unset( $this->
cache[$evictKey] );
316 unset( $this->timestamps[$evictKey] );
325 private function ping( $key ) {
326 $item = $this->
cache[$key];
327 unset( $this->
cache[$key] );
328 $this->
cache[$key] = $item;
336 private function getAge( $key, $field =
null ) {
337 if ( $field !==
null ) {
338 $mtime = $this->timestamps[$key][self::FIELDS][$field] ??
$this->epoch;
340 $mtime = $this->timestamps[$key][self::SIMPLE] ??
$this->epoch;
348 'entries' => $this->
cache,
349 'timestamps' => $this->timestamps
355 $this->
cache = $data[
'entries'] ?? [];
356 $this->timestamps = $data[
'timestamps'] ?? [];
365 return $this->wallClockOverride ?: microtime(
true );
373 $this->wallClockOverride =&
$time;