Debian Codegraph @

extensions/Echo/includes/CachedList.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use UnexpectedValueException;
6 use Wikimedia\ObjectCache\WANObjectCache;
7
8 /**
9 * Caches an ContainmentList within WANObjectCache to prevent needing
10 * to load the nested list from a potentially slow source (mysql, etc).
11 */
12 class CachedList implements ContainmentList {
13 private const ONE_WEEK = 4233600;
14
15 /** @var WANObjectCache */
16 protected $cache;
17 /** @var string */
18 protected $partialCacheKey;
19 /** @var ContainmentList */
20 protected $nestedList;
21 /** @var int */
22 protected $timeout;
23 /** @var string[]|null */
24 private $result;
25
26 /**
27 * @param WANObjectCache $cache Bag to stored cached data in.
28 * @param string $partialCacheKey Partial cache key, $nestedList->getCacheKey() will be appended
29 * to this to construct the cache key used.
30 * @param ContainmentList $nestedList The nested EchoContainmentList to cache the result of.
31 * @param int $timeout How long in seconds to cache the nested list, defaults to 1 week.
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 * @inheritDoc
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 * @inheritDoc
72 */
73 public function getCacheKey() {
74 return $this->cache->makeGlobalKey(
75 'echo-containment-list',
76 $this->partialCacheKey,
77 $this->nestedList->getCacheKey()
78 );
79 }
80 }