2018-01-21 20:52:43 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
|
|
|
use UnexpectedValueException;
|
2024-10-19 22:55:03 +00:00
|
|
|
use Wikimedia\ObjectCache\WANObjectCache;
|
2022-11-13 06:43:40 +00:00
|
|
|
|
2018-01-21 20:52:43 +00:00
|
|
|
/**
|
2022-11-13 06:43:40 +00:00
|
|
|
* Caches an ContainmentList within WANObjectCache to prevent needing
|
2018-01-21 20:52:43 +00:00
|
|
|
* to load the nested list from a potentially slow source (mysql, etc).
|
|
|
|
*/
|
2022-11-13 06:43:40 +00:00
|
|
|
class CachedList implements ContainmentList {
|
2020-09-19 18:55:32 +00:00
|
|
|
private const ONE_WEEK = 4233600;
|
2018-01-21 20:52:43 +00:00
|
|
|
|
2019-02-28 21:17:15 +00:00
|
|
|
/** @var WANObjectCache */
|
2018-01-21 20:52:43 +00:00
|
|
|
protected $cache;
|
2019-02-28 21:17:15 +00:00
|
|
|
/** @var string */
|
2018-01-21 20:52:43 +00:00
|
|
|
protected $partialCacheKey;
|
2022-11-13 06:43:40 +00:00
|
|
|
/** @var ContainmentList */
|
2018-01-21 20:52:43 +00:00
|
|
|
protected $nestedList;
|
2019-02-28 21:17:15 +00:00
|
|
|
/** @var int */
|
2018-01-21 20:52:43 +00:00
|
|
|
protected $timeout;
|
2019-02-28 21:17:15 +00:00
|
|
|
/** @var string[]|null */
|
2018-01-21 20:52:43 +00:00
|
|
|
private $result;
|
|
|
|
|
|
|
|
/**
|
2019-02-28 21:17:15 +00:00
|
|
|
* @param WANObjectCache $cache Bag to stored cached data in.
|
2018-01-21 20:52:43 +00:00
|
|
|
* @param string $partialCacheKey Partial cache key, $nestedList->getCacheKey() will be appended
|
|
|
|
* to this to construct the cache key used.
|
2022-11-13 06:43:40 +00:00
|
|
|
* @param ContainmentList $nestedList The nested EchoContainmentList to cache the result of.
|
2018-01-21 20:52:43 +00:00
|
|
|
* @param int $timeout How long in seconds to cache the nested list, defaults to 1 week.
|
|
|
|
*/
|
2019-02-28 21:17:15 +00:00
|
|
|
public function __construct(
|
|
|
|
WANObjectCache $cache,
|
|
|
|
$partialCacheKey,
|
2022-11-13 06:43:40 +00:00
|
|
|
ContainmentList $nestedList,
|
2019-02-28 21:17:15 +00:00
|
|
|
$timeout = self::ONE_WEEK
|
|
|
|
) {
|
2018-01-21 20:52:43 +00:00
|
|
|
$this->cache = $cache;
|
|
|
|
$this->partialCacheKey = $partialCacheKey;
|
|
|
|
$this->nestedList = $nestedList;
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getValues() {
|
|
|
|
if ( $this->result ) {
|
|
|
|
return $this->result;
|
|
|
|
}
|
2018-05-25 17:47:33 +00:00
|
|
|
$this->result = $this->cache->getWithSetCallback(
|
|
|
|
$this->getCacheKey(),
|
|
|
|
$this->timeout,
|
|
|
|
function () {
|
|
|
|
$result = $this->nestedList->getValues();
|
|
|
|
if ( !is_array( $result ) ) {
|
2023-06-09 00:21:09 +00:00
|
|
|
throw new UnexpectedValueException( sprintf(
|
2018-05-25 17:47:33 +00:00
|
|
|
"Expected array but received '%s' from '%s::getValues'",
|
2024-08-12 20:56:28 +00:00
|
|
|
get_debug_type( $result ),
|
2018-05-25 17:47:33 +00:00
|
|
|
get_class( $this->nestedList )
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
);
|
2018-02-05 19:56:54 +00:00
|
|
|
return $this->result;
|
2018-01-21 20:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getCacheKey() {
|
2019-02-28 21:17:15 +00:00
|
|
|
return $this->cache->makeGlobalKey(
|
|
|
|
'echo-containment-list',
|
|
|
|
$this->partialCacheKey,
|
|
|
|
$this->nestedList->getCacheKey()
|
|
|
|
);
|
2018-01-21 20:52:43 +00:00
|
|
|
}
|
|
|
|
}
|