1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Cache; |
4
|
|
|
|
5
|
|
|
use Ask\Language\Description\Description; |
6
|
|
|
use Ask\Language\Option\QueryOptions; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use OutOfBoundsException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @licence GPLv2+ |
14
|
|
|
* @author Thomas Pellissier Tanon |
15
|
|
|
*/ |
16
|
|
|
class EntityIdForQueryCache { |
17
|
|
|
|
18
|
|
|
const CACHE_ID_PREFIX = 'wikibase-store-entityforquery-'; |
19
|
|
|
|
20
|
|
|
const CACHE_LIFE_TIME = 86400; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Cache |
24
|
|
|
*/ |
25
|
|
|
private $cache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $lifeTime; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Cache $cache |
34
|
|
|
* @param int $lifeTime |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct( Cache $cache, $lifeTime = 0 ) { |
37
|
6 |
|
$this->cache = $cache; |
38
|
6 |
|
$this->lifeTime = $lifeTime; |
39
|
6 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Description $description |
43
|
|
|
* @param QueryOptions $queryOptions |
44
|
|
|
* @param string $entityType |
45
|
|
|
* @return EntityId[] |
46
|
|
|
*/ |
47
|
4 |
|
public function fetch( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
48
|
4 |
|
$result = $this->cache->fetch( $this->getCacheId( $description, $queryOptions, $entityType ) ); |
49
|
|
|
|
50
|
4 |
|
if( $result === false ) { |
51
|
2 |
|
throw new OutOfBoundsException( 'The search is not in the cache.' ); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Description $description |
59
|
|
|
* @param QueryOptions $queryOptions |
60
|
|
|
* @param string $entityType |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
2 |
|
public function contains( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
64
|
2 |
|
return $this->cache->contains( $this->getCacheId( $description, $queryOptions, $entityType ) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Description $description |
69
|
|
|
* @param QueryOptions $queryOptions |
70
|
|
|
* @param string $entityType |
71
|
|
|
* @param EntityId[] $entityIds |
72
|
|
|
*/ |
73
|
4 |
|
public function save( Description $description, QueryOptions $queryOptions = null, $entityType, array $entityIds ) { |
74
|
4 |
|
if( !$this->cache->save( |
75
|
4 |
|
$this->getCacheId( $description, $queryOptions, $entityType ), |
76
|
4 |
|
$entityIds, |
77
|
4 |
|
$this->lifeTime |
78
|
4 |
|
) ) { |
79
|
|
|
throw new RuntimeException( 'The cache failed to save.' ); |
80
|
|
|
} |
81
|
4 |
|
} |
82
|
|
|
|
83
|
6 |
|
private function getCacheId( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
84
|
6 |
|
$key = self::CACHE_ID_PREFIX . WIKIBASE_DATAMODEL_VERSION . '-' . |
85
|
6 |
|
$entityType . '-' . $description->getHash(); |
86
|
|
|
|
87
|
6 |
|
if( $queryOptions !== null ) { |
88
|
2 |
|
$key .= '-' . $queryOptions->getOffset() . '-' . $queryOptions->getLimit(); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
6 |
|
return $key; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|