Beispiel #1
0
 /**
  * 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;
     }
     if ($response = $this->storage->fetch($request)) {
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$this->canResponseSatisfyFailedRequest($request, $response)) {
             return;
         }
         $request->getParams()->set('cache.hit', 'error');
         $request->setResponse($response);
         $this->addResponseHeaders($request, $response);
         $event->stopPropagation();
     }
 }
Beispiel #2
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);
         }
     }
 }