protected function setUp() { $this->poolManager = new PoolManager(); $this->pool = $this->getMockBuilder(TaggablePoolInterface::class)->getMock(); $this->keyGenerator = $this->getMockBuilder(KeyGeneratorInterface::class)->getMock(); $this->keyGenerator->method('generateKey')->willReturn('key'); $this->poolManager->addPool('pool', $this->pool); $this->interceptor = new CacheEvictInterceptor($this->poolManager, $this->keyGenerator); }
/** * @param CacheEvict $annotation * @param InterceptionSuffixInterface $interception * * @return Result\ResultInterface */ public function interceptSuffix(CacheAnnotationInterface $annotation, InterceptionSuffixInterface $interception) : ResultInterface { $key = $this->calculateKey($annotation, $interception); foreach ($annotation->pools as $poolName) { $pool = $this->poolManager->getPool($poolName); $this->evictKey($pool, $key); $this->evictTags($pool, $annotation->tags); } return new EvictResult($interception, $key, $annotation->pools, $annotation->tags); }
/** * @param CacheUpdate $annotation * @param InterceptionSuffixInterface $interception * * @return ResultInterface */ public function interceptSuffix(CacheAnnotationInterface $annotation, InterceptionSuffixInterface $interception) : ResultInterface { if (!$interception->getReturnValue()) { return new EmptyResult(); } $key = $this->calculateKey($annotation, $interception); $item = new CacheItem($key); $item->set($interception->getReturnValue()); $item->setTags($annotation->tags); if ($annotation->ttl > 0) { $item->expiresAfter($annotation->ttl); } foreach ($annotation->pools as $poolName) { $pool = $this->poolManager->getPool($poolName); $pool->save($item); } return new UpdateResult($interception, $key, $annotation->pools); }
/** * @param $poolName * @param $key * * @return null|\Psr\Cache\CacheItemInterface * @throws RuntimeException */ private function locateCachedItem($poolName, $key) { $pool = $this->poolManager->getPool($poolName); if (!$pool->hasItem($key)) { return null; } $item = $pool->getItem($key); if (!$item->isHit()) { return null; } return $item; }
/** * @param $cacheKey * * @return \Psr\Cache\CacheItemInterface */ private function getCacheItem($cacheKey) { return $this->poolManager->getPool('books')->getItem($cacheKey); }