Example #1
0
 /**
  * @param  null|array $arguments Must be serializable.
  * @return void
  */
 public function warm($arguments = null)
 {
     $callable = $this->callable;
     if (is_array($arguments)) {
         $value = call_user_func_array($callable, $arguments);
     } else {
         $value = $callable();
     }
     $cacheKey = Cache::makeCacheKey($this->name, $arguments);
     $this->storage->addItem($cacheKey, serialize($value));
 }
Example #2
0
 /**
  * @param ResponseInterface $response
  * @param ItemInterface     $key
  * @param array|null        $data
  *
  * @throws \RuntimeException
  *
  * @return array
  */
 protected function handleSuccessfulResponse(\Zend\Http\Response $response, $key)
 {
     switch ((int) $response->getStatusCode()) {
         case 200:
             $data = json_decode($response->getBody(), true);
             $this->addItem($key, $data);
             return $data;
         case 304:
             return $this->cache->addItem($key);
         case 404:
             return null;
         default:
             throw new \RuntimeException('No support added for HTTP Status Code ' . $response->getStatusCode());
     }
 }
 public function testAddItemSetsTTL()
 {
     $capabilities = $this->_storage->getCapabilities();
     if ($capabilities->getMinTtl() === 0) {
         $this->markTestSkipped("Adapter doesn't support item expiration");
     }
     $ttl = $capabilities->getTtlPrecision();
     $this->_options->setTtl($ttl);
     $this->waitForFullSecond();
     $this->assertTrue($this->_storage->addItem('key', 'value'));
     // wait until the item expired
     $wait = $ttl + $capabilities->getTtlPrecision();
     usleep($wait * 2000000);
     if (!$capabilities->getUseRequestTime()) {
         $this->assertFalse($this->_storage->hasItem('key'));
     } else {
         $this->assertTrue($this->_storage->hasItem('key'));
     }
 }
Example #4
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);
             }
         }
     }
 }
Example #5
0
 public function testAddItemReturnsFalseIfNonWritable()
 {
     $this->_options->setWritable(false);
     $this->assertFalse($this->_storage->addItem('key', 'value'));
     $this->assertFalse($this->_storage->hasItem('key'));
 }
 /**
  * @test
  */
 public function delegatesAddItem()
 {
     $this->storage->addItem('cacheKey', 'value')->willReturn(true);
     $return = $this->cache->addItem('cacheKey', 'value');
     $this->assertTrue($return);
 }
 public function addItem($key, $value)
 {
     return $this->storage->addItem($key, $value);
 }