コード例 #1
0
ファイル: Cache.php プロジェクト: toin0u/concise
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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);
 }
コード例 #3
0
 /**
  * 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);
 }
コード例 #4
0
ファイル: Access.php プロジェクト: ming-hai/XoopsCore
 /**
  * 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;
 }
コード例 #5
0
 /**
  * {@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();
 }
コード例 #6
0
ファイル: CacheSpec.php プロジェクト: toin0u/concise
 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');
 }
コード例 #7
0
ファイル: PackageDownloader.php プロジェクト: brad-jones/ppm
 /**
  * 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;
 }
コード例 #8
0
ファイル: ComposerFileReader.php プロジェクト: brad-jones/ppm
 /**
  * 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;
 }
コード例 #9
0
ファイル: AbstractClient.php プロジェクト: RobinRadic/bnet
 /**
  * @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);
 }
コード例 #10
0
 /**
  * @return ItemInterface
  */
 private function getCachedRates()
 {
     return $this->pool->getItem('ongr_currency');
 }
コード例 #11
0
 /**
  * Returns settings cache item.
  *
  * @return ItemInterface
  */
 protected function getCache()
 {
     return $this->pool->getItem('ongr_settings.settings_cache', join($this->profiles, ','));
 }
コード例 #12
0
 /**
  * @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);
 }