| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Cache; |
| 4 |
|
| 5 |
use Iterator; |
| 6 |
use MapCacheLRU; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
abstract class LocalCache { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private const TARGET_MAX_NUM = 1000; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $targets; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private $lookups = []; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract protected function resolve( array $lookups ); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct() { |
| 47 |
$this->targets = new MapCacheLRU( self::TARGET_MAX_NUM ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function add( $key ) { |
| 56 |
if ( |
| 57 |
count( $this->lookups ) < self::TARGET_MAX_NUM |
| 58 |
&& !$this->targets->get( (string)$key ) |
| 59 |
) { |
| 60 |
$this->lookups[$key] = true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function get( $key ) { |
| 71 |
$target = $this->targets->get( (string)$key ); |
| 72 |
if ( $target ) { |
| 73 |
return $target; |
| 74 |
} |
| 75 |
|
| 76 |
if ( isset( $this->lookups[ $key ] ) ) { |
| 77 |
|
| 78 |
$targets = $this->resolve( array_keys( $this->lookups ) ); |
| 79 |
foreach ( $targets as $id => $val ) { |
| 80 |
$this->targets->set( $id, $val ); |
| 81 |
} |
| 82 |
$this->lookups = []; |
| 83 |
$target = $this->targets->get( (string)$key ); |
| 84 |
if ( $target ) { |
| 85 |
return $target; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function clearAll() { |
| 96 |
$this->targets->clear(); |
| 97 |
$this->lookups = []; |
| 98 |
} |
| 99 |
|
| 100 |
} |