public function testIsNoCache() { $request = new Request(); $isNoCache = $request->isNoCache(); $this->assertFalse($isNoCache); }
/** * Store a response generated by a request. * * @param Request $request * @param Response $response * @param integer $ttl */ public function cache(Request $request, Response $response, $ttl = null) { // only cache GET and HEAD requests if (!in_array($request->getMethod(), array('GET', 'HEAD'))) { return; } // respect Cache-Control: no-cache if ($request->isNoCache()) { return; } // skip already cached response if ($response->headers->has('X-ServerCache-Key')) { return; } // set cache lifetime if (is_null($ttl)) { $ttl = $this->defaultTtl; } $expirationDate = date_create('NOW + ' . $ttl . ' seconds'); // save cache item $key = $this->getCacheKey($request); $metadataReponse = new MetadataResponse($response, array("expirationDate" => $expirationDate)); $item = new CacheItem($key, true, $metadataReponse); $item->expiresAt($expirationDate); $this->cache->save($item); }
/** * Lookups a Response from the cache for the given Request. * * When a matching cache entry is found and is fresh, it uses it as the * response without forwarding any request to the backend. When a matching * cache entry is found but is stale, it attempts to "validate" the entry with * the backend using conditional GET. When no matching cache entry is found, * it triggers "miss" processing. * * @param Request $request A Request instance * @param bool $catch whether to process exceptions * * @return Response A Response instance * * @throws \Exception */ protected function lookup(Request $request, $catch = false) { // if allow_reload and no-cache Cache-Control, allow a cache reload if ($this->options['allow_reload'] && $request->isNoCache()) { $this->record($request, 'reload'); return $this->fetch($request, $catch); } try { $entry = $this->store->lookup($request); } catch (\Exception $e) { $this->record($request, 'lookup-failed'); if ($this->options['debug']) { throw $e; } return $this->pass($request, $catch); } if (null === $entry) { $this->record($request, 'miss'); return $this->fetch($request, $catch); } if (!$this->isFreshEnough($request, $entry)) { $this->record($request, 'stale'); return $this->validate($request, $entry, $catch); } $this->record($request, 'fresh'); $entry->headers->set('Age', $entry->getAge()); return $entry; }