|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types=1 ); |
|
4
|
|
|
namespace Wikibase\Lib\FormatterCache; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
8
|
|
|
use Wikibase\DataModel\Term\TermFallback; |
|
9
|
|
|
use Wikibase\Lib\Store\TermCacheKeyBuilder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* TermFallbackCacheFacade is class to allow for simplified |
|
13
|
|
|
* interaction with the shared cache used for storing |
|
14
|
|
|
* TermFallback objects (also known as the formatter cache). |
|
15
|
|
|
* |
|
16
|
|
|
* The cache returns TermFallbackCacheFacade::NO_VALUE in the |
|
17
|
|
|
* case there is no entry in the cache. |
|
18
|
|
|
* |
|
19
|
|
|
* Storing null values is also allowed as this indicates we |
|
20
|
|
|
* have already checked the database for the term but found nothing. |
|
21
|
|
|
* |
|
22
|
|
|
* @license GPL-2.0-or-later |
|
23
|
|
|
*/ |
|
24
|
|
|
class TermFallbackCacheFacade { |
|
25
|
|
|
|
|
26
|
|
|
use TermFallbackSerializerTrait; |
|
27
|
|
|
use TermCacheKeyBuilder; |
|
28
|
|
|
|
|
29
|
|
|
public const NO_VALUE = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CacheInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $cache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
private $cacheTtlInSeconds; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
CacheInterface $cache, |
|
43
|
|
|
int $cacheTtlInSeconds |
|
44
|
|
|
) { |
|
45
|
|
|
$this->cache = $cache; |
|
46
|
|
|
$this->cacheTtlInSeconds = $cacheTtlInSeconds; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param EntityId $targetEntityId |
|
51
|
|
|
* @param int $revisionId |
|
52
|
|
|
* @param string $languageCode |
|
53
|
|
|
* @param string $termType |
|
54
|
|
|
* @return false|TermFallback|null |
|
55
|
|
|
* |
|
56
|
|
|
* @example returning null indicates we have already checked the database and stored it's null response in the cache. |
|
57
|
|
|
* @example returning false indicates there is no cache entry. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function get( EntityId $targetEntityId, int $revisionId, string $languageCode, string $termType ) { |
|
60
|
|
|
$cacheKey = $this->buildCacheKey( $targetEntityId, $revisionId, $languageCode, $termType ); |
|
61
|
|
|
$termFallback = $this->cache->get( $cacheKey, self::NO_VALUE ); |
|
62
|
|
|
|
|
63
|
|
|
if ( $termFallback === self::NO_VALUE ) { |
|
64
|
|
|
return self::NO_VALUE; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->unserialize( $termFallback ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param null|TermFallback $termFallback |
|
72
|
|
|
* @param EntityId $targetEntityId |
|
73
|
|
|
* @param int $revisionId |
|
74
|
|
|
* @param string $languageCode |
|
75
|
|
|
* @param string $termType |
|
76
|
|
|
*/ |
|
77
|
|
|
public function set( |
|
78
|
|
|
?TermFallback $termFallback, |
|
79
|
|
|
EntityId $targetEntityId, |
|
80
|
|
|
int $revisionId, |
|
81
|
|
|
string $languageCode, |
|
82
|
|
|
string $termType |
|
83
|
|
|
): void { |
|
84
|
|
|
$cacheKey = $this->buildCacheKey( $targetEntityId, $revisionId, $languageCode, $termType ); |
|
85
|
|
|
$serialization = $this->serialize( $termFallback ); |
|
86
|
|
|
|
|
87
|
|
|
$this->cache->set( $cacheKey, $serialization, $this->cacheTtlInSeconds ); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|