/**
  * @test
  */
 public function delegatesGetOptions()
 {
     $options = ['foo'];
     $this->storage->getOptions()->willReturn($options);
     $return = $this->cache->getOptions();
     $this->assertSame($options, $return);
 }
示例#2
0
 /**
  * @note ZF2 doesn't support per-item ttl storage therefore we use
  * https://github.com/zendframework/zf2/pull/5386#issuecomment-43191005
  *
  * - ttl with 0 means an item never expires
  *
  * @since 1.1
  *
  * {@inheritDoc}
  */
 public function save($id, $data, $ttl = 0)
 {
     $options = $this->cache->getOptions();
     $oldTtl = $options->getTtl($ttl);
     $options->setTtl($ttl);
     try {
         $this->cacheInserts++;
         $this->cache->setItem($id, $data);
     } catch (\Exception $e) {
         // Don't re-throw any exception, the consumer
         // should not care about this
         $this->cacheInserts--;
     }
     $options->setTtl($oldTtl);
 }
 /**
  * Persists a cache item immediately.
  *
  * @param CacheItemInterface $item
  *   The cache item to save.
  *
  * @return bool
  *   True if the item was successfully persisted. False if there was an error.
  */
 public function save(CacheItemInterface $item)
 {
     if (!$item instanceof CacheItem) {
         throw new InvalidArgumentException('$item must be an instance of ' . CacheItem::class);
     }
     $this->validateKey($item->getKey());
     try {
         $options = false;
         $expiration = $item->getExpiration();
         // @todo I can't see any way to set the TTL on an individual item except by temporarily overwriting the
         //       option on the storage adapter. Not sure if all storage adapters will support this...
         if ($expiration instanceof DateTime) {
             $options = $this->storage->getOptions();
             $new = clone $options;
             $interval = $expiration->diff(new DateTime(), true);
             $new->setTtl($interval->format('%s'));
             $this->storage->setOptions($new);
         }
         $saved = $this->storage->setItem($item->getKey(), $item->get());
         if ($options) {
             $this->storage->setOptions($options);
         }
     } catch (Exception\InvalidArgumentException $e) {
         throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
     } catch (Exception\ExceptionInterface $e) {
         throw new CacheException($e->getMessage(), $e->getCode(), $e);
     }
     return $saved;
 }
 public function testSetGetHasAndRemoveItemsWithoutNamespace()
 {
     $this->_storage->getOptions()->setNamespace('');
     $items = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');
     $this->assertSame(array(), $this->_storage->setItems($items));
     $rs = $this->_storage->getItems(array_keys($items));
     $this->assertInternalType('array', $rs);
     foreach ($items as $key => $value) {
         $this->assertArrayHasKey($key, $rs);
         $this->assertEquals($value, $rs[$key]);
     }
     $rs = $this->_storage->hasItems(array_keys($items));
     $this->assertInternalType('array', $rs);
     $this->assertEquals(count($items), count($rs));
     foreach ($items as $key => $value) {
         $this->assertContains($key, $rs);
     }
     $this->assertSame(array('missing'), $this->_storage->removeItems(array('missing', 'key1', 'key3')));
     unset($items['key1'], $items['key3']);
     $rs = $this->_storage->getItems(array_keys($items));
     $this->assertInternalType('array', $rs);
     foreach ($items as $key => $value) {
         $this->assertArrayHasKey($key, $rs);
         $this->assertEquals($value, $rs[$key]);
     }
     $rs = $this->_storage->hasItems(array_keys($items));
     $this->assertInternalType('array', $rs);
     $this->assertEquals(count($items), count($rs));
     foreach ($items as $key => $value) {
         $this->assertContains($key, $rs);
     }
 }
示例#5
0
 /**
  * @param $cacheKey
  * @param Closure $closure
  * @param null $lifetime
  * @return mixed
  */
 public function getItem($cacheKey, Closure $closure, $lifetime = null)
 {
     // we have to check if we enable the caching in config
     if (!$this->isCachingEnable()) {
         return $closure();
     }
     $data = $this->cachingService->getItem($cacheKey);
     if (!$data) {
         $data = $closure();
         if ($lifetime > 0) {
             $this->cachingService->setOptions($this->cachingService->getOptions()->setTtl($lifetime));
         }
         $this->cachingService->setItem($cacheKey, $data);
     }
     return $data;
 }
示例#6
0
 /**
  * Sets the zend-cache storage namespace.
  *
  * @param string $serviceName
  */
 private function setNamespace($serviceName = null)
 {
     if ($serviceName === null) {
         $this->storage->getOptions()->setNamespace($this->defaultNamespace);
     } else {
         $this->storage->getOptions()->setNamespace($serviceName);
     }
 }
示例#7
0
 /**
  * @param  string $name name of lock
  * @param  bool   $blocking
  * @return bool
  */
 protected function getLock($name, $blocking)
 {
     if ($this->isLocked($name)) {
         return false;
     }
     $options = $this->cache->getOptions();
     $oldTTL = 0;
     if (!empty($options->getTtl())) {
         $oldTTL = $options->getTtl();
     }
     $options->setTtl($this->expiration);
     if (!$this->cache->setItem($name, serialize($this->getLockInformation()))) {
         $options->setTtl($oldTTL);
         return false;
     }
     $this->locks[$name] = $name;
     return true;
 }
示例#8
0
 /**
  * Object destructor
  *
  * Clean up cache storage
  */
 public function __destruct()
 {
     if ($this->cache !== null) {
         if ($this->cache instanceof ClearByNamespaceCacheStorage) {
             $this->cache->clearByNamespace($this->cache->getOptions()->getNamespace());
         } elseif ($this->cache instanceof FlushableCacheStorage) {
             $this->cache->flush();
         }
     }
 }
示例#9
0
 public function testOptionsFluentInterface()
 {
     $options = $this->_storage->getOptions();
     foreach ($options->toArray() as $option => $value) {
         $method = ucwords(str_replace('_', ' ', $option));
         $method = 'set' . str_replace(' ', '', $method);
         $this->assertSame($options, $options->{$method}($value), "Method '{$method}' doesn't implement the fluent interface");
     }
     $this->assertSame($this->_storage, $this->_storage->setOptions($options), "Method 'setOptions' doesn't implement the fluent interface");
 }
示例#10
0
 /**
  * Triggered after controller and view calls
  *
  * @param FilterResponseEvent $event
  */
 public function onKernelResponse(FilterResponseEvent $event)
 {
     $request = $event->getRequest();
     $response = $event->getResponse();
     if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST && $request->isMethodSafe()) {
         $key = $this->getKeyFromRequest($request);
         $response->setLastModified(new \DateTime());
         // If no cache request header exists - do nothing
         if ($request->isNoCache() || !$response->isSuccessful()) {
             return;
             //TODO redirects cache
         }
         // If response does not exists - put it to cache
         if (!$this->storage->hasItem($key)) {
             $response->setTtl($this->storage->getOptions()->getTtl());
             $data = ['content' => $response->getContent(), 'status' => $response->getStatusCode(), 'headers' => $response->headers->all()];
             $this->storage->addItem($key, $data);
             if ($this->storage instanceof TaggableInterface) {
                 $this->storage->setTags($key, $tags);
             }
         }
     }
 }
示例#11
0
 public function getOptions()
 {
     return $this->storage->getOptions();
 }