/** * @param string $pathInfo * * @return array */ public function match($pathInfo) { $host = strtr($this->context->getHost(), '.', '_'); $method = strtolower($this->context->getMethod()); $key = 'route_' . $method . '_' . $host . '_' . $pathInfo; if ($this->cache->hasItem($key)) { return $this->cache->getItem($key)->get(); } $match = parent::match($pathInfo); $item = $this->cache->getItem($key); $item->set($match)->expiresAfter(self::CACHE_LIFETIME); return $match; }
public function fetch($source, EncapsulatedOptions $options = null) { $optionsCopy = $options ? $options->copy() : []; if ($this->isCacheEnabled()) { ksort($optionsCopy); $hash = $this->hash([$source, $optionsCopy]); if ($this->cache->hasItem($hash)) { return $this->cache->getItem($hash)->get(); } } $data = $this->fetchFreshData($source, $options); isset($hash) && $this->cache->save($this->cache->getItem($hash)->set($data)); return $data; }
/** * Confirms if the cache contains specified cache item. * * Note: This method MAY avoid retrieving the cached value for performance reasons. * This could result in a race condition with CacheItemInterface::get(). To avoid * such situation use CacheItemInterface::isHit() instead. * * @param string $key * The key for which to check existence. * * @throws InvalidArgumentException * If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException * MUST be thrown. * * @return bool * True if item exists in the cache, false otherwise. */ public function hasItem($key) { if ($this->local->hasItem($key) || $this->remote->hasItem($key)) { return true; } return false; }
public function getMetadata(string $entityClassName) : Entity { if (array_key_exists($entityClassName, $this->metadata)) { return $this->metadata[$entityClassName]; } $cacheKey = sprintf('simplefm.metadata.%s', md5($entityClassName)); if ($this->cache->hasItem($cacheKey)) { return $this->metadata[$entityClassName] = $this->cache->getItem($cacheKey)->get(); } $xmlPath = sprintf('%s/%s', $this->xmlFolder, $this->buildFilename($entityClassName)); if (!file_exists($xmlPath)) { throw InvalidFileException::fromNonExistentFile($xmlPath, $entityClassName); } $entityMetadata = $this->buildMetadata($xmlPath); $this->cache->save(new CacheItem($cacheKey, true, $entityMetadata)); return $this->metadata[$entityClassName] = $entityMetadata; }
/** * {@inheritdoc} */ public function getRouteCollection() { $key = 'route_collection'; if (null === $this->collection) { if ($this->cache->hasItem($key)) { $collection = $this->cache->getItem($key)->get(); if ($collection !== null) { $this->collection = $collection; return $this->collection; } } $this->collection = parent::getRouteCollection(); $item = $this->cache->getItem($key); $item->set($this->collection)->expiresAfter(self::CACHE_LIFETIME); } return $this->collection; }
/** * Fetch cached data from storage. * * @param string $gatewayId The gateway ID. * @param string $hash The data hash. * * @return DataContainer|null The data container or null if no data available * * @throws \InvalidArgumentException * * @since 1.0 */ public function get($gatewayId, $hash) { $key = $this->createKey($gatewayId, $hash); if (!$this->cache->hasItem($key)) { return null; } $item = $this->cache->getItem($key); return $item->get(); }
/** * @param string $key * @param array $tags * * @return bool true if parents are valid */ private function validateParents($key, array $tags) { $parts = $this->explodeKey($key); $parentKey = ''; foreach ($parts as $part) { $parentKey .= $part; if (!$this->cache->hasItem($parentKey, $tags)) { // Invalid item return false; } } return true; }
/** * Sends the API request if cache not hit * * @param string $method * @param string $uri * @param null $body * @param array $headers * @param string $cacheKey * @param integer $ttl * * @return array */ public function send($method, $uri, $body = null, array $headers = [], $cacheKey = null, $ttl = null) { $saveToCache = false; if ($cacheKey !== null && $ttl !== null && $this->cache) { if ($this->cache->hasItem($cacheKey)) { return $this->cache->getItem($cacheKey)->get(); } else { $saveToCache = true; } } if (is_array($body)) { $body = http_build_query($body); $headers['Content-Type'] = 'application/x-www-form-urlencoded'; } $request = MessageFactoryDiscovery::find()->createRequest($method, $this->getApiUrl($uri), $headers, $body); $rawResponse = $this->getHttpClient()->sendRequest($request); $data = $this->processResponse($rawResponse); if ($this->cache && true === $saveToCache) { $item = $this->cache->getItem($cacheKey)->set($data)->expiresAfter($ttl); $this->cache->save($item); } return $data; }
/** * @param Client $client * @param $cacheKey * @param AbstractApiRequest $request * @param int $ttl * @return \Commercetools\Core\Model\Common\JsonDeserializeInterface|null */ protected function retrieve(Client $client, $cacheKey, AbstractApiRequest $request, $locale, $force = false, $ttl = self::CACHE_TTL) { if (!$force && $this->enableCache && $this->cache->hasItem($cacheKey)) { $cachedData = $this->cache->getItem($cacheKey); if (empty($cachedData)) { throw new NotFoundHttpException("resource not found"); } $result = unserialize($cachedData->get()); $result->setContext($client->getConfig()->getContext()); } else { $response = $request->executeWithClient($client); if ($response->isError() || is_null($response->toObject())) { $this->store($cacheKey, '', $ttl); throw new NotFoundHttpException("resource not found"); } $result = $request->mapFromResponse($response, $this->getMapper($locale)); $this->store($cacheKey, serialize($result), $ttl); } return $result; }
/** * {@inheritdoc} */ public function has($class) { return $this->cacheItemPool->hasItem($this->escapeClassName($class)); }
/** * @param CacheItemPoolInterface $pool * @param $key */ private function evictKey(CacheItemPoolInterface $pool, $key) { if (!$key || !$pool->hasItem($key)) { return; } $pool->deleteItem($key); }
/** * {@inheritdoc} */ public function hasItem($key) { return $this->pool->hasItem($key); }
/** * @inheritDoc */ public function has(string $key) : bool { return $this->pool->hasItem($key); }
/** * @param string $secret * @param int $code * @return bool */ public function authenticate($secret, $code) { $correct = false; for ($i = -1; $i <= 1; $i++) { if ($this->calculateCode($secret) == $code) { $correct = true; break; } } // If they're not using a cache to prevent people being able to use the same code twice or if they were wrong anyway... if (!isset($this->cachePool) || $correct == false) { return $correct; } // If we're here then we must be using a cache, and we must be right // We generate the key as securely as possible, then salt it using something that will always be replicatable. // We're doing this hashing for de-duplication (aka, we want to know if it exists), but as we're also possibly // securing the secret somewhere, we want to try and have as secure as possible // // If someone has any better suggestions on how to achieve this, please send in a PR! :P $key = crypt($secret . "|" . $code, md5($code)); // If it existed, then we want this function to return false if ($this->cachePool->hasItem($key)) { return false; } // If it didn't, then we want this function to add it to the cache // In PSR-6 getItem will always contain an CacheItemInterface and that seems to be the only way to add stuff // to the cachePool $item = $this->cachePool->getItem($key); // It's a quick expiry thing, 30 seconds is more than long enough $item->expiresAfter(new \DateInterval("PT30S")); // We don't care about the value at all, it's just something that's needed to use the caching interface $item->set(true); $this->cachePool->save($item); return true; }