/**
  * Creates a request to use for revalidation
  *
  * @param RequestInterface $request  Request
  * @param Response         $response Response to revalidate
  *
  * @return RequestInterface returns a revalidation request
  */
 protected function createRevalidationRequest(RequestInterface $request, Response $response)
 {
     $revalidate = clone $request;
     $revalidate->removeHeader('Pragma')->removeHeader('Cache-Control')->setHeader('If-Modified-Since', $response->getLastModified() ?: $response->getDate());
     if ($response->getEtag()) {
         $revalidate->setHeader('If-None-Match', '"' . $response->getEtag() . '"');
     }
     // Remove any cache plugins that might be on the request
     $revalidate->getEventDispatcher()->removeSubscriber($this->plugin);
     return $revalidate;
 }
Example #2
0
 /**
  * Creates a request to use for revalidation
  *
  * @param RequestInterface $request  Request
  * @param Response         $response Response to revalidate
  *
  * @return RequestInterface returns a revalidation request
  */
 protected function createRevalidationRequest(RequestInterface $request, Response $response)
 {
     $revalidate = clone $request;
     $revalidate->removeHeader('Pragma')->removeHeader('Cache-Control');
     if ($response->getLastModified()) {
         $revalidate->setHeader('If-Modified-Since', $response->getLastModified());
     }
     if ($response->getEtag()) {
         $revalidate->setHeader('If-None-Match', $response->getEtag());
     }
     // Remove any cache plugins that might be on the request to prevent infinite recursive revalidations
     $dispatcher = $revalidate->getEventDispatcher();
     foreach ($dispatcher->getListeners() as $eventName => $listeners) {
         foreach ($listeners as $listener) {
             if (is_array($listener) && $listener[0] instanceof CachePlugin) {
                 $dispatcher->removeListener($eventName, $listener);
             }
         }
     }
     return $revalidate;
 }
 /**
  * If possible, return a cache response on an error
  *
  * @param Event $event
  */
 public function onRequestError(Event $event)
 {
     $request = $event['request'];
     if (!$this->canCache->canCacheRequest($request)) {
         return;
     }
     $cacheKey = $this->keyProvider->getCacheKey($request);
     if ($cachedData = $this->storage->fetch($cacheKey)) {
         $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
         $response->setRequest($request);
         $response->setHeader('Age', time() - strtotime($response->getLastModified() ?: $response->getDate() ?: 'now'));
         if ($this->canResponseSatisfyFailedRequest($request, $response)) {
             $request->getParams()->set('cache.hit', 'error');
             $this->addResponseHeaders($cacheKey, $request, $response);
             $event['response'] = $response;
             $event->stopPropagation();
         }
     }
 }