| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
use UnexpectedValueException; |
| 6 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class CachedList implements ContainmentList { |
| 13 |
private const ONE_WEEK = 4233600; |
| 14 |
|
| 15 |
|
| 16 |
protected $cache; |
| 17 |
|
| 18 |
protected $partialCacheKey; |
| 19 |
|
| 20 |
protected $nestedList; |
| 21 |
|
| 22 |
protected $timeout; |
| 23 |
|
| 24 |
private $result; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct( |
| 34 |
WANObjectCache $cache, |
| 35 |
$partialCacheKey, |
| 36 |
ContainmentList $nestedList, |
| 37 |
$timeout = self::ONE_WEEK |
| 38 |
) { |
| 39 |
$this->cache = $cache; |
| 40 |
$this->partialCacheKey = $partialCacheKey; |
| 41 |
$this->nestedList = $nestedList; |
| 42 |
$this->timeout = $timeout; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function getValues() { |
| 49 |
if ( $this->result ) { |
| 50 |
return $this->result; |
| 51 |
} |
| 52 |
$this->result = $this->cache->getWithSetCallback( |
| 53 |
$this->getCacheKey(), |
| 54 |
$this->timeout, |
| 55 |
function () { |
| 56 |
$result = $this->nestedList->getValues(); |
| 57 |
if ( !is_array( $result ) ) { |
| 58 |
throw new UnexpectedValueException( sprintf( |
| 59 |
"Expected array but received '%s' from '%s::getValues'", |
| 60 |
get_debug_type( $result ), |
| 61 |
get_class( $this->nestedList ) |
| 62 |
) ); |
| 63 |
} |
| 64 |
return $result; |
| 65 |
} |
| 66 |
); |
| 67 |
return $this->result; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function getCacheKey() { |
| 74 |
return $this->cache->makeGlobalKey( |
| 75 |
'echo-containment-list', |
| 76 |
$this->partialCacheKey, |
| 77 |
$this->nestedList->getCacheKey() |
| 78 |
); |
| 79 |
} |
| 80 |
} |