/**
  * @test
  */
 public function delegatesSetOptions()
 {
     $options = ['foo'];
     $return = $this->cache->setOptions($options);
     $this->assertSame($this->cache, $return);
     $this->storage->setOptions($options)->shouldHaveBeenCalled();
 }
 /**
  * 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;
 }
 /**
  * @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;
 }
Exemple #4
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");
 }
 public function setOptions($options)
 {
     $this->storage->setOptions($options);
     return $this;
 }