/** * Get value from cache * * @param \Itkg\Core\CacheableInterface $item * * @return string */ public function get(CacheableInterface $item) { if (false !== ($result = parent::get($item))) { return $result; } return $this->adapter->get($item); }
/** * @return mixed */ public function getFirstItem() { $keys = $this->cacheAdapter->get($this->createCacheItem()); if (!is_array($keys)) { return null; } return array_pop($keys); }
/** * @param string $key */ public function removeItem($key) { $values = $this->cacheAdapter->get($this->createCacheItem()); if (!isset($values[$key])) { return; } unset($values[$key]); $this->cacheAdapter->set($this->createCacheItem($values)); }
/** * Load data from cache * * @param EntityLoadEvent $event * * @return void */ public function fetchEntityFromCache(EntityLoadEvent $event) { $entity = $event->getEntity(); // Check cache if (false !== ($data = $this->cache->get($entity))) { // Set data from cache to entity object $entity->setDataFromCache($data); $entity->setIsLoaded(true); $this->dispatcher->dispatch('cache.load', new CacheEvent($entity)); } }
/** * @param callable $logCallback */ public function processAll(\Closure $logCallback = null) { while (null !== ($key = $this->reader->getFirstItem())) { try { $cachedService = $this->cacheAdapter->get($this->createCacheItem($key)); $this->writer->removeItem($key); if ($cachedService instanceof AbstractService) { $service = $this->services->getServiceByIdentifier($cachedService->getIdentifier()); $this->cacheWarmer->warm($service, $cachedService); $this->log($logCallback, sprintf('SUCCESS ON REFRESH KEY %s', $key)); } } catch (\Exception $e) { $this->log($logCallback, sprintf('ERROR ON REFRESH KEY %s : %s', $key, $e->getMessage())); } } }
public function testCleanByTagsWithIntersectOnlyAndWildcardComparisonAndOneTag() { $item = new CacheableData('key2', null, 'toto'); $this->adapter->set($item); $item2 = new CacheableData('key3', null, 'toto2'); $this->adapter->set($item2); $this->assertEquals('toto', $this->adapter->get($item)); $this->manager->addKeyToTag('version', 150, 'key2'); $this->manager->addKeyToTag('country', 'FR', 'key2'); $this->manager->addKeyToTag('version', 1500, 'key3'); $this->manager->addKeyToTag('country', 'FR', 'key3'); $this->manager->cleanByTags(array('version' => array('value' => 10, 'type' => 'wildcard')), true); $this->assertEquals('toto', $this->adapter->get($item)); $this->assertEquals('toto2', $this->adapter->get($item2)); $this->manager->cleanByTags(array('version' => array('value' => 150, 'type' => 'wildcard')), true); $this->assertFalse($this->adapter->get($item)); $this->assertFalse($this->adapter->get($item2)); }
/** * @param array $tags * @param bool $withIntersection * * @return array */ private function getKeysByTags(array $tags, $withIntersection = false) { $index = 0; $keys = array(); foreach ($tags as $tag) { $item = $this->createItem($tag); $tagKeys = unserialize($this->cacheAdapter->get($item)); if (!is_array($tagKeys)) { $tagKeys = array(); } if ($withIntersection) { if ($index === 0) { $keys = $tagKeys; } else { $keys = array_intersect($keys, $tagKeys); } } else { $keys = array_merge($keys, $tagKeys); } $index++; } return $keys; }
/** * @param Request $request * @param array $params * * @return mixed */ public function readCache(Request $request, array $params = array()) { return $this->cacheAdapter->get($this->createCacheableData($request)); }