Example #1
0
 public function fetch(RequestInterface $request)
 {
     $key = $this->getCacheKey($request);
     if (!($entries = $this->cache->fetch($key))) {
         return null;
     }
     $match = null;
     $headers = $this->persistHeaders($request);
     $entries = unserialize($entries);
     foreach ($entries as $index => $entry) {
         if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
             $match = $entry;
             break;
         }
     }
     if (!$match) {
         return null;
     }
     // Ensure that the response is not expired
     $response = null;
     if ($match[4] < time()) {
         $response = -1;
     } else {
         $response = new Response($match[2], $match[1]);
         if ($match[3]) {
             if ($body = $this->cache->fetch($match[3])) {
                 $response->setBody($body);
             } else {
                 // The response is not valid because the body was somehow deleted
                 $response = -1;
             }
         }
     }
     if ($response === -1) {
         // Remove the entry from the metadata and update the cache
         unset($entries[$index]);
         if ($entries) {
             $this->cache->save($key, serialize($entries));
         } else {
             $this->cache->delete($key);
         }
         return null;
     }
     return $response;
 }