/**
  * Save item to cache
  * @param string $key
  * @param mixed $value
  * @param array $arr_options - Optional
  * Options: ttl => Overwrite configured ttl with set value. Reverts to configured ttl once completed
  */
 public function setCacheItem($key, $value, $arr_options = array())
 {
     //check if caching is enabled
     $arr_config = $this->getServiceLocator()->get("config");
     if ($arr_config["front_end_application_config"]["cache_enabled"] == FALSE) {
         return FALSE;
     }
     //end if
     //adjust key
     $key = $this->setIdentifier($key);
     try {
         /**
          * Overwrite ttl
          */
         if (is_numeric($arr_options["ttl"])) {
             $old_ttl = $this->storageFactory->getOptions()->getTtl();
             $this->storageFactory->getOptions()->setTtl((int) $arr_options["ttl"]);
             $this->storageFactory->setItem($key, $value);
             //reset ttl
             $this->storageFactory->getOptions()->setTtl($old_ttl);
             return;
         }
         //end if
         $this->storageFactory->setItem($key, $value);
     } catch (\Exception $e) {
         trigger_error($e->getMessage(), E_USER_WARNING);
     }
     //end catch
 }
 public function testGetOptions()
 {
     $this->_storage = $this->getMockForAbstractAdapter();
     $options = $this->_storage->getOptions();
     $this->assertInstanceOf('Zend\\Cache\\Storage\\Adapter\\AdapterOptions', $options);
     $this->assertInternalType('boolean', $options->getWritable());
     $this->assertInternalType('boolean', $options->getReadable());
     $this->assertInternalType('integer', $options->getTtl());
     $this->assertInternalType('string', $options->getNamespace());
     $this->assertInternalType('string', $options->getKeyPattern());
 }
 /**
  * normalizes the cache id for zend cache
  *
  * @param string $cacheId The cache id
  *
  * @return string The formated cache id
  */
 protected function normalizeKey($cacheId)
 {
     $cacheId = parent::normalizeKey($cacheId);
     if (($pattern = $this->cache->getOptions()->getKeyPattern()) && !preg_match($pattern, $cacheId)) {
         $pattern = str_replace(array('^[', '*$'), array('[^', ''), $pattern);
         $cacheId = preg_replace($pattern, '_', $cacheId);
     }
     return $cacheId;
 }
 /**
  * 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;
     }
 }
Exemple #5
0
 /**
  * Set Cache Adapter
  *
  * @param \Zend\Cache\Storage\Adapter\AbstractAdapter $cacheAdapter
  */
 public function setCacheAdapter($cacheAdapter)
 {
     $this->cacheAdapter = $cacheAdapter;
     $this->cacheAdapter->getOptions()->setTtl($this->getCacheOptions()->getExpires());
 }