| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\LightweightObjectStore\ExpirationAwareness; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class MapCacheLRU implements ExpirationAwareness { |
| 35 |
|
| 36 |
private $cache = []; |
| 37 |
|
| 38 |
private $timestamps = []; |
| 39 |
|
| 40 |
private $epoch; |
| 41 |
|
| 42 |
|
| 43 |
private $maxCacheKeys; |
| 44 |
|
| 45 |
|
| 46 |
private $wallClockOverride; |
| 47 |
|
| 48 |
|
| 49 |
private const RANK_TOP = 1.0; |
| 50 |
|
| 51 |
|
| 52 |
private const SIMPLE = 0; |
| 53 |
|
| 54 |
private const FIELDS = 1; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function __construct( int $maxKeys ) { |
| 60 |
if ( $maxKeys <= 0 ) { |
| 61 |
throw new InvalidArgumentException( '$maxKeys must be above zero' ); |
| 62 |
} |
| 63 |
|
| 64 |
$this->maxCacheKeys = $maxKeys; |
| 65 |
|
| 66 |
$this->epoch = $this->getCurrentTime(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function newFromArray( array $values, $maxKeys ) { |
| 76 |
$mapCache = new self( $maxKeys ); |
| 77 |
$mapCache->cache = ( count( $values ) > $maxKeys ) |
| 78 |
? array_slice( $values, -$maxKeys, null, true ) |
| 79 |
: $values; |
| 80 |
|
| 81 |
return $mapCache; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function toArray() { |
| 89 |
return $this->cache; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function set( $key, $value, $rank = self::RANK_TOP ) { |
| 108 |
if ( $this->has( $key ) ) { |
| 109 |
$this->ping( $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] ); |
| 114 |
} |
| 115 |
|
| 116 |
if ( $rank < 1.0 && $rank > 0 ) { |
| 117 |
$offset = intval( $rank * count( $this->cache ) ); |
| 118 |
$this->cache = array_slice( $this->cache, 0, $offset, true ) |
| 119 |
+ [ $key => $value ] |
| 120 |
+ array_slice( $this->cache, $offset, null, true ); |
| 121 |
} else { |
| 122 |
$this->cache[$key] = $value; |
| 123 |
} |
| 124 |
|
| 125 |
$this->timestamps[$key] = [ |
| 126 |
self::SIMPLE => $this->getCurrentTime(), |
| 127 |
self::FIELDS => [] |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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.' ); |
| 143 |
} |
| 144 |
return array_key_exists( $key, $this->cache ) |
| 145 |
&& ( |
| 146 |
|
| 147 |
$maxAge === INF |
| 148 |
|| $maxAge <= 0 |
| 149 |
|| $this->getKeyAge( $key ) <= $maxAge |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function get( $key, $maxAge = INF, $default = null ) { |
| 170 |
if ( !$this->has( $key, $maxAge ) ) { |
| 171 |
return $default; |
| 172 |
} |
| 173 |
|
| 174 |
$this->ping( $key ); |
| 175 |
|
| 176 |
return $this->cache[$key]; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function setField( $key, $field, $value, $initRank = self::RANK_TOP ) { |
| 186 |
if ( $this->has( $key ) ) { |
| 187 |
$this->ping( $key ); |
| 188 |
|
| 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)" ); |
| 192 |
} |
| 193 |
} else { |
| 194 |
$this->set( $key, [], $initRank ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( !is_string( $field ) && !is_int( $field ) ) { |
| 198 |
throw new UnexpectedValueException( |
| 199 |
__METHOD__ . ": invalid field for '$key'; must be string or integer." ); |
| 200 |
} |
| 201 |
|
| 202 |
$this->cache[$key][$field] = $value; |
| 203 |
$this->timestamps[$key][self::FIELDS][$field] = $this->getCurrentTime(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function hasField( $key, $field, $maxAge = INF ) { |
| 214 |
$value = $this->get( $key ); |
| 215 |
|
| 216 |
if ( !is_int( $field ) && !is_string( $field ) ) { |
| 217 |
throw new UnexpectedValueException( |
| 218 |
__METHOD__ . ": invalid field for '$key'; must be string or integer." ); |
| 219 |
} |
| 220 |
return is_array( $value ) |
| 221 |
&& array_key_exists( $field, $value ) |
| 222 |
&& ( |
| 223 |
$maxAge === INF |
| 224 |
|| $maxAge <= 0 |
| 225 |
|| $this->getKeyFieldAge( $key, $field ) <= $maxAge |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function getField( $key, $field, $maxAge = INF ) { |
| 237 |
if ( !$this->hasField( $key, $field, $maxAge ) ) { |
| 238 |
return null; |
| 239 |
} |
| 240 |
|
| 241 |
return $this->cache[$key][$field]; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function getAllKeys() { |
| 249 |
return array_keys( $this->cache ); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function getWithSetCallback( |
| 266 |
$key, callable $callback, $rank = self::RANK_TOP, $maxAge = INF |
| 267 |
) { |
| 268 |
if ( $this->has( $key, $maxAge ) ) { |
| 269 |
$value = $this->get( $key ); |
| 270 |
} else { |
| 271 |
$value = $callback(); |
| 272 |
if ( $value !== false ) { |
| 273 |
$this->set( $key, $value, $rank ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return $value; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function makeKey( ...$components ) { |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
$key = ''; |
| 294 |
foreach ( $components as $i => $component ) { |
| 295 |
if ( $i > 0 ) { |
| 296 |
$key .= ':'; |
| 297 |
} |
| 298 |
$key .= strtr( $component, [ '%' => '%25', ':' => '%3A' ] ); |
| 299 |
} |
| 300 |
return $key; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function clear( $keys = null ) { |
| 310 |
if ( func_num_args() == 0 ) { |
| 311 |
$this->cache = []; |
| 312 |
$this->timestamps = []; |
| 313 |
} else { |
| 314 |
foreach ( (array)$keys as $key ) { |
| 315 |
unset( $this->cache[$key] ); |
| 316 |
unset( $this->timestamps[$key] ); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function getMaxSize() { |
| 328 |
return $this->maxCacheKeys; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function setMaxSize( int $maxKeys ) { |
| 339 |
if ( $maxKeys <= 0 ) { |
| 340 |
throw new InvalidArgumentException( '$maxKeys must be above zero' ); |
| 341 |
} |
| 342 |
|
| 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] ); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private function ping( $key ) { |
| 357 |
$item = $this->cache[$key]; |
| 358 |
unset( $this->cache[$key] ); |
| 359 |
$this->cache[$key] = $item; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private function getKeyAge( $key ) { |
| 367 |
$mtime = $this->timestamps[$key][self::SIMPLE] ?? $this->epoch; |
| 368 |
|
| 369 |
return ( $this->getCurrentTime() - $mtime ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
private function getKeyFieldAge( $key, $field ) { |
| 378 |
$mtime = $this->timestamps[$key][self::FIELDS][$field] ?? $this->epoch; |
| 379 |
|
| 380 |
return ( $this->getCurrentTime() - $mtime ); |
| 381 |
} |
| 382 |
|
| 383 |
public function __serialize() { |
| 384 |
return [ |
| 385 |
'entries' => $this->cache, |
| 386 |
'timestamps' => $this->timestamps, |
| 387 |
'maxCacheKeys' => $this->maxCacheKeys, |
| 388 |
]; |
| 389 |
} |
| 390 |
|
| 391 |
public function __unserialize( $data ) { |
| 392 |
$this->cache = $data['entries'] ?? []; |
| 393 |
$this->timestamps = $data['timestamps'] ?? []; |
| 394 |
|
| 395 |
$this->maxCacheKeys = $data['maxCacheKeys'] ?? ( count( $this->cache ) + 1 ); |
| 396 |
$this->epoch = $this->getCurrentTime(); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
protected function getCurrentTime() { |
| 404 |
return $this->wallClockOverride ?: microtime( true ); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public function setMockTime( &$time ) { |
| 412 |
$this->wallClockOverride =& $time; |
| 413 |
} |
| 414 |
} |