/** * Update a valid non cacheable Response with http cache headers * * @see http://symfony.com/fr/doc/current/book/http_cache.html */ public function handleResponse(Response $response) { // do not handle invalid response if (!$response->isOk()) { return $response; } // do not handle response with http cache headers if ($response->isCacheable()) { return $response; } // seek for optional configuration $this->readRoutingConfiguration(); // mark the response as private $response->setPrivate(); // set the private or shared max age $response->setMaxAge($this->duration); $response->setSharedMaxAge($this->duration); // set expires $date = new \DateTime(); $date->modify(sprintf('+%d seconds', $this->duration)); $response->setExpires($date); // set a custom Cache-Control directive $response->headers->addCacheControlDirective('must-revalidate', true); return $response; }
/** * Parses the requested route to fetch * - the resource (databox, basket, record etc ..) * - general action (list, add, search) * - the action (setstatus, setname etc..) * - the aspect (collections, related, content etc..) * * @param ApiLog $log * @param Request $request * @param Response $response */ private function setDetails(ApiLog $log, Request $request, Response $response) { $chunks = explode('/', trim($request->getPathInfo(), '/')); if (false === $response->isOk() || sizeof($chunks) === 0) { return; } switch ($chunks[0]) { case ApiLog::DATABOXES_RESOURCE: $this->hydrateDataboxes($log, $chunks); break; case ApiLog::RECORDS_RESOURCE: $this->hydrateRecords($log, $chunks); break; case ApiLog::BASKETS_RESOURCE: $this->hydrateBaskets($log, $chunks); break; case ApiLog::FEEDS_RESOURCE: $this->hydrateFeeds($log, $chunks); break; case ApiLog::QUARANTINE_RESOURCE: $this->hydrateQuarantine($log, $chunks); break; case ApiLog::STORIES_RESOURCE: $this->hydrateStories($log, $chunks); break; case ApiLog::MONITOR_RESOURCE: $this->hydrateMonitor($log, $chunks); break; } }
public function after(Request $request, Response $response) { $now = new \DateTime("now"); $mapper = new CacheRecordSQLMapper($this->app["db"]); $currentRouteName = $request->attributes->get("_route"); if ($this->isCacheCompromiser($currentRouteName)) { /** @todo method check */ $mapper = new CacheRecordSQLMapper($this->app["db"]); $compromisedCount = $mapper->compromiseAll(); return; } if ($this->isRouteCacheable($currentRouteName) && $response->isOk()) { $cacheRecord = $mapper->findByUri($this->getCurrentUri()); if (!$cacheRecord) { // $this->createCacheRecord(); $data = []; $data["uri"] = $this->getCurrentUri(); $data["hash"] = md5($response->getContent()); $data["lmt"] = $now->format("Y-m-d H:i:s"); $cacheRecord = new CacheRecord($data); $mapper->create($cacheRecord); $response->setPublic(); $response->setDate($now); $response->setMaxAge($this->config->getMaxAge()); $response->setSharedMaxAge($this->config->getMaxAgeShared()); $response->setLastModified($now); $response->headers->addCacheControlDirective('must-revalidate', true); return $response; } if ($cacheRecord->getCompromised()) { if ($cacheRecord->getHash() === md5($response->getContent())) { $cacheRecord->setCompromised(false); $mapper->update($cacheRecord->getId(), $cacheRecord); $response = $this->createResponse($cacheRecord->getLmt()); $response->setNotModified(); return $response; } else { $cacheRecord->setCompromised(false); $cacheRecord->setHash(md5($response->getContent())); $cacheRecord->setLmt(date("Y-m-d H:i:s")); $mapper->update($cacheRecord->getId(), $cacheRecord); return $response; } } else { $now = new \DateTime("2015-12-12"); $response->setPublic(); $response->setDate($now); $response->setMaxAge($this->config->getMaxAge()); $response->setSharedMaxAge($this->config->getMaxAgeShared() + 1); $response->setLastModified(new \DateTime($cacheRecord->getLmt())); return $response; } } }
public static function afterRead(Request $request, Response $response, Application $app) { if ($response->isOk()) { $token = self::getCacheToken($request, $app); if (!$app['cache']->contains($token)) { if (self::isFileListRequest($request)) { $minutes = $app['config']->getMinutesCachingFileListings(); } else { $minutes = $app['config']->getMinutesCachingData(); } $app['cache']->save($token, $response->getContent(), $minutes * 60); } } }
/** * {@inheritdoc} */ public function processResponse(Request $request, Response $response) { if (!$response->isOk()) { return; } $cacheKey = $request->attributes->get('_cacheKey'); $ttl = $request->attributes->get('_cacheTTL'); if (empty($cacheKey)) { return; } $cache = $this->getCache(); $this->info(sprintf('miss: save into cache: %s', $cacheKey), ['application' => 'cache', 'type' => 'miss', 'cacheKey' => $cacheKey, 'ttl' => $ttl]); $item = $cache->getItem($cacheKey); $item->set($response); $item->expiresAfter($ttl); $cache->save($item); $response->headers->set('X-Cache', 'miss'); $response->setMaxAge($ttl); $response->setExpires(new DateTime(sprintf('+%d seconds', $ttl))); }
public function testIsOk() { $response = new Response('', 200); $this->assertTrue($response->isOk()); $response = new Response('', 404); $this->assertFalse($response->isOk()); }
private function assertResponseOk(Response $response) { $this->assertTrue($response->isOk()); $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response); }