/** * Displays a list of the most recent feeds, reverse date order, to the user. * * It's intended as a starting off point to the rest of the application. * * @return array|ViewModel */ public function indexAction() { if (!is_null($this->_cache)) { if (!$this->_cache->hasItem(self::KEY_ALL_RESULTS)) { $resultset = $this->_feedTable->fetchMostRecentFeeds(self::DEFAULT_FEED_COUNT); $this->_cache->setItem(self::KEY_ALL_RESULTS, $resultset->toArray()); } else { $resultset = $this->_cache->getItem(self::KEY_ALL_RESULTS); } } else { $resultset = $this->_feedTable->fetchMostRecentFeeds(self::DEFAULT_FEED_COUNT); } $paginator = $this->getPaginator($resultset); $paginator->setCurrentPageNumber($this->params()->fromRoute('page', self::DEFAULT_PAGE)); $paginator->setItemCountPerPage($this->params()->fromRoute('perPage', self::DEFAULT_RECORDS_PER_PAGE)); return new ViewModel(array('paginator' => $paginator, 'feedCount' => self::DEFAULT_FEED_COUNT, 'messages' => array('info' => $this->flashMessenger()->hasInfoMessages(), 'error' => $this->flashMessenger()->hasErrorMessages()))); }
public function testHasItemCallsInternalHasItem() { $this->_storage = $this->getMockForAbstractAdapter(array('internalHasItem')); $key = 'key1'; $result = true; $this->_storage->expects($this->once())->method('internalHasItem')->with($this->equalTo($key))->will($this->returnValue($result)); $rs = $this->_storage->hasItem($key); $this->assertSame($result, $rs); }
/** * Test if an item exists. * * @param string $cacheId * * @return bool */ public function hasItem($cacheId) { $cacheId = $this->normalizeKey($cacheId); try { return $this->cache->hasItem($cacheId); } catch (ZendException\ExceptionInterface $ex) { return false; } }
/** * @inheritdoc */ public function build() { if ($this->built) { return; } $key = 'assets-manager'; if ($this->cacheAdapter->hasItem($key)) { $data = $this->cacheAdapter->getItem($key); $assetManager = unserialize($data); if ($assetManager instanceof AssetManager) { $this->setAssetManager($assetManager); return; } } parent::build(); $this->cacheAdapter->setItem($key, serialize($this->getAssetManager())); $this->built = true; }
/** * Cache Response for future requests * * @param MvcEvent $e * @return \Zend\Stdlib\ResponseInterface */ public function onFinish(MvcEvent $e) { $request = $e->getRequest(); if (!$request instanceof HttpRequest) { return; } if (!$request->isGet()) { return; } $response = $e->getResponse(); if ($response instanceof HttpResponse && !$response->isOk()) { return; } // Do not continue if weren't able to compose a key if (empty($this->cache_key)) { return; } if (!$this->cacheAdapter->hasItem($this->cache_key)) { $resourceIdentifier = $e->getRouteMatch()->getParam('resource'); $resource = call_user_func($this->getResourceLocatorService(), $resourceIdentifier); if (!$resource instanceof Resource || !$resource->isCacheable()) { return; } // Generate Response cache headers based on Resource CacheOptions $cacheOptions = $resource->getCacheOptions(); $cacheControl = new CacheControl(); $cacheControl->addDirective($cacheOptions->getAccess()); $cacheControl->addDirective('max-age', $cacheOptions->getMaxAge()); $cacheControl->addDirective('expires', $cacheOptions->getExpires()); $cacheControl->addDirective('must-revalidate'); $dateTime = new \DateTime(); $dateTime->modify('+ ' . $cacheOptions->getExpires() . 'seconds'); $expires = new Expires(); $expires->setDate($dateTime); $lastModified = new LastModified(); $lastModified->setDate(new \DateTime()); // Add Headers to Response Header $response->getHeaders()->addHeader($cacheControl); $response->getHeaders()->addHeader($expires); $response->getHeaders()->addHeaderLine('Pragma: ' . $cacheOptions->getAccess()); $response->getHeaders()->addHeader(Etag::fromString('Etag: ' . md5($response->getBody()))); $response->getHeaders()->addHeader($lastModified); // Set cache adapter's TTL using Resource cache expires value $this->cacheAdapter->getOptions()->setTtl($cacheOptions->getExpires()); $this->cacheAdapter->setItem($this->cache_key, $response); //return $response; } }
/** * @param $id * * @return array|\ArrayObject|bool|null */ public function getEntityByIdentifier($id) { if ($this->cacheAdapter instanceof CacheAdapter) { if ($this->cacheAdapter->hasItem($this->getCacheKeyHash($id))) { $entity = $this->cacheAdapter->getItem($this->getCacheKeyHash($id)); return $entity; } } $rowset = $this->select(array($this->getIdentifierName() => $id)); $entity = $rowset->current(); if (!$entity) { return false; } if ($this->cacheAdapter instanceof CacheAdapter) { $this->cacheAdapter->setItem($this->getCacheKeyHash($id), $entity); } return $entity; }
/** * Compile to file. If destination is not specified return CSS. * * @param string $source * @param string|null $destination * @param array|string|null $include Include path(s) to use * @throws \Exception on compilation error * @return string Compiled CSS */ public function compile($source, $destination = null, $include = null) { $include = array_merge($this->includes, (array) $include); $useCache = null == $destination && null !== $this->cache; if ($useCache) { if ($this->cache->hasItem($source)) { return $this->cache->getItem($source); } } $arguments = implode(' ', array_map('escapeshellarg', $this->arguments)); $arguments .= ' ' . $this->getCommandArguments($source, $destination, $include); $command = "{$this->compiler} {$arguments}" . ' 2>&1'; $result = exec($command, $output, $exitCode); if ($exitCode != 0) { throw new \Exception(sprintf('Error compiling CSS "%s": %s', $source, implode(PHP_EOL, $output))); } $css = implode(PHP_EOL, $output); if ($useCache) { $this->cache->setItem($source, $css); } return $css; }
/** * Test if an item exists. * * @param string $key * @return bool * @throws Exception\ExceptionInterface * * @triggers hasItem.pre(PreEvent) * @triggers hasItem.post(PostEvent) * @triggers hasItem.exception(ExceptionEvent) */ public function hasItem($key) { $options = $this->getOptions(); if ($options->getReadable() && $options->getClearStatCache()) { clearstatcache(); } return parent::hasItem($key); }
/** * Test if an item exists. * * Options: * - ttl <int> optional * - The time-to-life (Default: ttl of object) * - namespace <string> optional * - The namespace to use (Default: namespace of object) * * @param string $key * @param array $options * @return boolean * @throws Exception\ExceptionInterface * * @triggers hasItem.pre(PreEvent) * @triggers hasItem.post(PostEvent) * @triggers hasItem.exception(ExceptionEvent) */ public function hasItem($key, array $options = array()) { $baseOptions = $this->getOptions(); if ($baseOptions->getReadable() && $baseOptions->getClearStatCache()) { clearstatcache(); } return parent::hasItem($key, $options); }
/** * @return mixed * @throws \Exception * @throws null * * Generic request */ public function request() { if ($this->method == null || empty($this->method)) { throw new \Exception('METHOD is not defined'); } if ($this->uri == null || empty($this->uri)) { throw new \Exception('URI is not defined'); } // return debug information if debug is enabled if ($this->debug) { return $this->debugInfo(); } try { // cache key $cacheKey = $this->generateCacheKey(); // cache if ($this->useCache) { // if cached response is found, return it formatted if ($this->cache->hasItem($cacheKey)) { $response = $this->cache->getItem($cacheKey); return $this->formatResponseToReturn($response); } } // log time (START) $timeStart = microtime(true); // return seeds if present if ($seeds = $this->findSeeds()) { return $seeds; } $request = (new ArtaxRequest())->setUri($this->uri)->setMethod($this->method)->setAllHeaders($this->headers); if ($this->params != null) { $request->setBody(json_encode($this->params)); } // make the request (first attempt) $ampResponse = $this->doRequest($request); // return response if it's not an ArtaxResponse (it could be a string cached in local file) if (!$ampResponse instanceof ArtaxResponse) { return $ampResponse; } // log time (END) $timeEnd = microtime(true); if ($this->config['newrelic'] === true && extension_loaded('newrelic')) { newrelic_custom_metric('Custom/Artax/Load_time', round($timeEnd - $timeStart)); } // code and body $response['code'] = (int) $ampResponse->getStatus(); $response['body'] = json_decode($ampResponse->getBody(), true); // optional headers foreach ($this->headersToReturn as $headerToReturn) { if ($ampResponse->hasHeader($headerToReturn)) { $response['headers'][$headerToReturn] = $ampResponse->getHeader($headerToReturn)[0]; } } // store response in cache if ($this->useCache) { $this->cache->setItem($cacheKey, $response); // TODO cache ttl (set timeout on specific cache key) if ($this->cacheTtl) { } } // seeds $this->writeSeeds($response); // reformat response return $this->formatResponseToReturn($response); } catch (\Exception $error) { throw new \Exception($error); } }