1 | <?php |
||
16 | class EntityDocumentCache { |
||
17 | |||
18 | const CACHE_ID_PREFIX = 'wikibase-store-entity-'; |
||
19 | |||
20 | /** |
||
21 | * @var Cache |
||
22 | */ |
||
23 | private $cache; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $lifeTime; |
||
29 | |||
30 | /** |
||
31 | * @param Cache $cache |
||
32 | * @param int $lifeTime |
||
33 | */ |
||
34 | 5 | public function __construct( Cache $cache, $lifeTime = 0 ) { |
|
38 | |||
39 | /** |
||
40 | * Returns an Entity from the cache |
||
41 | * |
||
42 | * @param EntityId $entityId |
||
43 | * @return EntityDocument|null |
||
44 | */ |
||
45 | 3 | public function fetch( EntityId $entityId ) { |
|
48 | |||
49 | /** |
||
50 | * Tests if an Entity exists in the cache. |
||
51 | * |
||
52 | * @param EntityId $entityId |
||
53 | * @return bool |
||
54 | */ |
||
55 | 2 | public function contains( $entityId ) { |
|
58 | |||
59 | /** |
||
60 | * Save an Entity in the cache. |
||
61 | * |
||
62 | * @param EntityDocument $entity |
||
63 | */ |
||
64 | 3 | public function save( EntityDocument $entity ) { |
|
65 | 3 | if( !$this->cache->save( |
|
66 | 3 | $this->getCacheIdFromEntityId( $entity->getId() ), |
|
|
|||
67 | 3 | $entity, |
|
68 | 3 | $this->lifeTime |
|
69 | 3 | ) ) { |
|
70 | throw new RuntimeException( 'The cache failed to save for entity ' . $entity->getId()->getSerialization() ); |
||
71 | } |
||
72 | 3 | } |
|
73 | |||
74 | 5 | private function getCacheIdFromEntityId( EntityId $entityId ) { |
|
77 | } |
||
78 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: