/** * Shorten or expand a URL checking it in the cache first * * @param string $url * @param string $transformation * * @return string */ protected function transformUrl($url, $transformation) { $cachedUrl = $this->pool->getItem(md5($url)); if ($cachedUrl->isMiss()) { $url = $this->provider->{$transformation}($url); $cachedUrl->set($url); } else { $url = $cachedUrl->get(); } return $url; }
/** * Returns a Cache item for the specified key. The key can be either a series of string arguments, * or an array. * * @internal param array|string $key , $key, $key... * * @return \Stash\Interfaces\ItemInterface */ public function getItem() { $args = func_get_args(); // check to see if a single array was used instead of multiple arguments, & check empty in case of empty clear() if (empty($args)) { $args = array(); } elseif (!isset($args[1]) && is_array($args[0])) { $args = $args[0]; } array_unshift($args, self::SPI_CACHE_KEY_PREFIX); return $this->cachePool->getItem($args); }
/** * Prepend key with prefix and support array format Stash supported before. * * {@see \Psr\Cache\CacheItemPoolInterface} * * @internal param array|string $key , $key, $key... * * @return \Stash\Interfaces\ItemInterface */ public function getItem() { $args = func_get_args(); if (empty($args)) { return $this->cachePool->getItem(self::SPI_CACHE_KEY_PREFIX); } // Upstream seems to no longer support array, so we flatten it if (!isset($args[1]) && is_array($args[0])) { $key = implode('/', array_map([$this, 'washKey'], $args[0])); } else { $key = '' . implode('/', array_map([$this, 'washKey'], $args)); } $key = $key === '' ? self::SPI_CACHE_KEY_PREFIX : self::SPI_CACHE_KEY_PREFIX . '/' . $key; return $this->cachePool->getItem($key); }
/** * cache block wrapper using Invalidation::PRECOMPUTE * * If the cache read for $key is a miss, call the $regenFunction to update it. * With the PRECOMPUTE strategy, it will trigger a miss on a read on one caller * before the cache expires, so it will be done in advance. * * @param string|string[] $cacheKey Identifier for the cache item * @param callable $regenFunction function to generate cached content * @param int|DateTime|null $ttl time to live, number ofseconds as integer, * DateTime to expire at a specific time, * or null for default * @param mixed ...$args variable argument list for $regenFunction * * @return mixed */ public function cacheRead($cacheKey, $regenFunction, $ttl = null, $args = null) { if (is_null($args)) { $varArgs = array(); } else { $varArgs = func_get_args(); array_shift($varArgs); // pull off $key array_shift($varArgs); // pull off $regenFunction array_shift($varArgs); // pull off $ttl } $item = $this->pool->getItem($cacheKey); // Get the data from cache using the Stash\Invalidation::PRECOMPUTE technique // for dealing with stampedes $cachedContent = $item->get(Invalidation::PRECOMPUTE); // Check to see if the cache missed, which could mean that it either didn't exist or was stale. if ($item->isMiss()) { // Mark this instance as the one regenerating the cache. $item->lock(); // Run the relatively expensive code. $cachedContent = call_user_func_array($regenFunction, $varArgs); // save result $item->set($cachedContent, $ttl); } return $cachedContent; }
/** * {@inheritdoc} */ public function removeRolePermission(Role $role, Permission $permission) { $this->driver->removeRolePermission($role, $permission); // Invalidate cache $cacheId = sprintf('roles/%s', $role->getRoleName()); $item = $this->pool->getItem($cacheId); $item->clear(); }
function it_expands_a_url(Provider $provider, PoolInterface $pool, ItemInterface $item) { $item->isMiss()->willReturn(true); $item->set('http://any.url')->shouldBeCalled(); $pool->getItem(md5('http://short.ly/1234'))->willReturn($item); $provider->expand('http://short.ly/1234')->willReturn('http://any.url'); $this->expand('http://short.ly/1234')->shouldReturn('http://any.url'); }
/** * Downloads the composer packages data from packagist.org * * @param string $packageName The name of composer package. * @return array The parsed json response. * * @see https://packagist.org/apidoc */ private function downloadPackagistPackageData($packageName) { $url = $this->packagistBaseUrl . $packageName . '.json'; $this->cache->setNamespace(sha1(__CLASS__)); $item = $this->cache->getItem(sha1($url)); $data = $item->get(); if ($item->isMiss()) { $item->lock(); $response = $this->http->request('GET', $url); $data = json_decode($response->getBody(), true)['package']; $this->cache->save($item->set($data)); } return $data; }
/** * Given the path to a composer file we return it's json as an array. * * @param string $file * @return array */ private function readFile($file) { $this->cache->setNamespace(sha1(__CLASS__)); $item = $this->cache->getItem(sha1($file)); $data = $item->get(); if ($item->isMiss()) { $item->lock(); if ($this->filesystem->has($file)) { $data = json_decode($this->filesystem->read($file), true); } else { throw new ComposerFileNotFound($this->filesystem->getAdapter()->applyPathPrefix($file)); } $this->cache->save($item->set($data)); } return $data; }
/** * @param string $url * @param array $options * * @return array */ protected function makeRequest($url, array $options = []) { if ($this->client === null) { $this->client = new GuzzleClient(); } $item = $this->cache->getItem($this->getRequestKey($url, $options)); $data = $item->get(); if ($item->isMiss() === false) { $options = array_replace_recursive($options, ['headers' => ['If-Modified-Since' => $data['modified']]]); } $options = array_replace_recursive($options, ['headers' => ['Accept' => 'application/json', 'User-Agent' => $this->getUserAgent()], 'query' => ['apikey' => $this->apiKey, 'locale' => $this->region->getLocale()]]); try { $response = $this->client->get($url, $options); } catch (ClientException $exception) { if ($exception->getCode() === 404) { return null; } throw new BattleNetException($exception->getResponse()->json()['detail'], $exception->getCode()); } return $this->handleSuccessfulResponse($response, $item, $data); }
/** * @return ItemInterface */ private function getCachedRates() { return $this->pool->getItem('ongr_currency'); }
/** * Returns settings cache item. * * @return ItemInterface */ protected function getCache() { return $this->pool->getItem('ongr_settings.settings_cache', join($this->profiles, ',')); }
/** * @param tubepress_api_url_UrlInterface $url * * @return \Stash\Interfaces\ItemInterface */ private function _getItem(tubepress_api_url_UrlInterface $url) { $key = str_replace('/', '~', "{$url}"); return $this->_apiCache->getItem($key); }