|
| 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 | + public function __construct( Cache $cache, $lifeTime = 0 ) { |
| 37 | + $this->cache = $cache; |
| 38 | + $this->lifeTime = $lifeTime; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param Description $description |
| 43 | + * @param QueryOptions $queryOptions |
| 44 | + * @param string $entityType |
| 45 | + * @return EntityId[] |
| 46 | + */ |
| 47 | + public function fetch( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
| 48 | + $result = $this->cache->fetch( $this->getCacheId( $description, $queryOptions, $entityType ) ); |
| 49 | + |
| 50 | + if( $result === false ) { |
| 51 | + throw new OutOfBoundsException( 'The search is not in the cache.' ); |
| 52 | + } |
| 53 | + |
| 54 | + return $result; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param Description $description |
| 59 | + * @param QueryOptions $queryOptions |
| 60 | + * @param string $entityType |
| 61 | + * @return boolean |
| 62 | + */ |
| 63 | + public function contains( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
| 64 | + 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 | + public function save( Description $description, QueryOptions $queryOptions = null, $entityType, array $entityIds ) { |
| 74 | + if( !$this->cache->save( |
| 75 | + $this->getCacheId( $description, $queryOptions, $entityType ), |
| 76 | + $entityIds, |
| 77 | + $this->lifeTime |
| 78 | + ) ) { |
| 79 | + throw new RuntimeException( 'The cache failed to save.' ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private function getCacheId( Description $description, QueryOptions $queryOptions = null, $entityType ) { |
| 84 | + $key = self::CACHE_ID_PREFIX . WIKIBASE_DATAMODEL_VERSION . '-' . |
| 85 | + $entityType . '-' . $description->getHash(); |
| 86 | + |
| 87 | + if( $queryOptions !== null ) { |
| 88 | + $key .= '-' . $queryOptions->getOffset() . '-' . $queryOptions->getLimit(); |
| 89 | + } |
| 90 | + |
| 91 | + return $key; |
| 92 | + } |
| 93 | +} |
0 commit comments