Example #1
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')->setHeader('If-Modified-Since', $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
 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());
     }
     $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;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function cache($key, Response $response, $ttl = null)
 {
     if ($ttl === null) {
         $ttl = $this->defaultTtl;
     }
     if ($ttl) {
         $response->setHeader('X-Guzzle-Cache', "key={$key}, ttl={$ttl}");
         // Remove excluded headers from the response  (see RFC 2616:13.5.1)
         foreach ($this->excludeResponseHeaders as $header) {
             $response->removeHeader($header);
         }
         // Add a Date header to the response if none is set (for validation)
         if (!$response->getDate()) {
             $response->setHeader('Date', Utils::getHttpDate('now'));
         }
         $this->cache->save($key, array($response->getStatusCode(), $response->getHeaders()->getAll(), $response->getBody(true)), $ttl);
     }
 }
 /**
  * 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 to prevent infinite recursive revalidations
     $dispatcher = $revalidate->getEventDispatcher();
     foreach ($dispatcher->getListeners() as $eventName => $listeners) {
         foreach ($listeners as $listener) {
             if ($listener[0] instanceof CachePlugin) {
                 $dispatcher->removeListener($eventName, $listener);
             }
         }
     }
     return $revalidate;
 }
 public function cache($key, Response $response, $ttl = null)
 {
     if ($ttl === null) {
         $ttl = $this->defaultTtl;
     }
     $ttl += $response->getMaxAge();
     if ($ttl) {
         $response->setHeader('X-Guzzle-Cache', "key={$key}; ttl={$ttl}");
         // Remove excluded headers from the response  (see RFC 2616:13.5.1)
         foreach ($this->excludeResponseHeaders as $header) {
             $response->removeHeader($header);
         }
         // Add a Date header to the response if none is set (for validation)
         if (!$response->getDate()) {
             $response->setHeader('Date', gmdate(ClientInterface::HTTP_DATE));
         }
         $this->cache->save($key, array($response->getStatusCode(), $response->getHeaders()->toArray(), $response->getBody(true)), $ttl);
     }
 }
Example #6
0
 /**
  * Check if a response in cache will satisfy the request before sending
  *
  * @param Event $event
  */
 public function onRequestBeforeSend(Event $event)
 {
     $request = $event['request'];
     if (!$this->canCache->canCacheRequest($request)) {
         return;
     }
     $hashKey = $this->keyProvider->getCacheKey($request);
     $this->cached[$request] = $hashKey;
     // If the cached data was found, then make the request into a
     // manually set request
     if ($cachedData = $this->storage->fetch($hashKey)) {
         unset($this->cached[$request]);
         $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]);
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$response->hasHeader('X-Guzzle-Cache')) {
             $response->setHeader('X-Guzzle-Cache', "key={$hashKey}");
         }
         // Validate that the response satisfies the request
         if ($this->canResponseSatisfyRequest($request, $response)) {
             $request->setResponse($response);
         }
     }
 }
Example #7
0
 /**
  * Save data to the cache adapter
  *
  * @param string   $key      The cache key
  * @param Response $response The response to cache
  * @param int      $lifetime Amount of seconds to cache
  *
  * @return int Returns the lifetime of the cached data
  */
 protected function saveCache($key, Response $response, $lifetime = null)
 {
     $lifetime = $lifetime ?: $this->defaultLifetime;
     // If the data is cacheable, then save it to the cache adapter
     if ($lifetime) {
         // Remove excluded headers from the response  (see RFC 2616:13.5.1)
         foreach ($this->excludeResponseHeaders as $header) {
             $response->removeHeader($header);
         }
         // Add a Date header to the response if none is set (for validation)
         if (!$response->getDate()) {
             $response->setHeader('Date', Utils::getHttpDate('now'));
         }
         $data = array('c' => $response->getStatusCode(), 'h' => $response->getHeaders(), 'b' => $response->getBody(true));
         if ($this->serialize) {
             $data = serialize($data);
         }
         $this->getCacheAdapter()->save($key, $data, $lifetime);
     }
     return $lifetime;
 }
 /**
  * If possible, set a cache response on a cURL exception
  *
  * @param Event $event
  *
  * @return null
  */
 public function onRequestException(Event $event)
 {
     if (!$event['exception'] instanceof CurlException) {
         return;
     }
     $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->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$this->canResponseSatisfyFailedRequest($request, $response)) {
             return;
         }
         $request->getParams()->set('cache.hit', 'error');
         $request->setResponse($response);
         $event->stopPropagation();
     }
 }