Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.33% |
11 / 15 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
ResultsCache | |
73.33% |
11 / 15 |
|
83.33% |
5 / 6 |
6.68 | |
0.00% |
0 / 1 |
getDefaultInstance | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
makeKey | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport\Api; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use Wikibase\DataModel\Entity\EntityId; |
7 | use Wikimedia\ObjectCache\WANObjectCache; |
8 | |
9 | /** |
10 | * A thin wrapper around a WANObjectCache |
11 | * that maps entity IDs to cache keys. |
12 | * |
13 | * @author Lucas Werkmeister |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class ResultsCache { |
17 | |
18 | /** |
19 | * @var WANObjectCache |
20 | */ |
21 | private $cache; |
22 | |
23 | /** |
24 | * @var string |
25 | */ |
26 | private $formatVersion; |
27 | |
28 | public static function getDefaultInstance() { |
29 | return new self( |
30 | MediaWikiServices::getInstance()->getMainWANObjectCache(), |
31 | 'v2.2' // .1: T188384; .2: T189593 |
32 | ); |
33 | } |
34 | |
35 | /** |
36 | * @param WANObjectCache $cache |
37 | * @param string $formatVersion The version of the API response format. |
38 | */ |
39 | public function __construct( WANObjectCache $cache, $formatVersion ) { |
40 | $this->cache = $cache; |
41 | $this->formatVersion = $formatVersion; |
42 | } |
43 | |
44 | /** |
45 | * @param EntityId $entityId |
46 | * @return string cache key |
47 | */ |
48 | public function makeKey( EntityId $entityId ) { |
49 | return $this->cache->makeKey( |
50 | 'WikibaseQualityConstraints', // extension |
51 | 'checkConstraints', // action |
52 | $this->formatVersion, // API response format version |
53 | $entityId->getSerialization() |
54 | ); |
55 | } |
56 | |
57 | /** |
58 | * @param EntityId $key |
59 | * @param mixed &$curTTL |
60 | * @param string[] $checkKeys |
61 | * @param array &$info |
62 | * @return mixed |
63 | */ |
64 | public function get( EntityId $key, &$curTTL = null, array $checkKeys = [], array &$info = [] ) { |
65 | return $this->cache->get( $this->makeKey( $key ), $curTTL, $checkKeys, $info ); |
66 | } |
67 | |
68 | /** |
69 | * @param EntityId $key |
70 | * @param mixed $value |
71 | * @param int $ttl |
72 | * @param array $opts |
73 | * @return bool |
74 | */ |
75 | public function set( EntityId $key, $value, $ttl = 0, array $opts = [] ) { |
76 | return $this->cache->set( $this->makeKey( $key ), $value, $ttl, $opts ); |
77 | } |
78 | |
79 | /** |
80 | * @param EntityId $key |
81 | * @return bool |
82 | */ |
83 | public function delete( EntityId $key ) { |
84 | return $this->cache->delete( $this->makeKey( $key ) ); |
85 | } |
86 | |
87 | } |