setTtl() public method

This method adjusts the Cache-Control/s-maxage directive.
public setTtl ( integer $seconds ) : Response
$seconds integer Number of seconds
return Response
Beispiel #1
0
 public function cachedPageAction(Request $request, Application $app)
 {
     // this will be cached for 10 sec
     $response = new Response($app['twig']->render('page-with-cache.html.twig', array('date' => date('Y-M-d h:i:s'))));
     $response->setTtl(10);
     return $response;
 }
 /**
  * Return the stylesheets for the Debugbar
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function css()
 {
     $renderer = $this->app['debugbar']->getJavascriptRenderer();
     $content = $renderer->dumpAssetsToString('css');
     $response = new Response($content, 200, array('Content-Type' => 'text/css'));
     $response->setTtl($this->ttl);
     return $response;
 }
 /**
  * Displays related content for given locationId, using legacy template.
  *
  * @param mixed $locationId
  *
  * @return Response
  */
 public function relatedContentAction($locationId)
 {
     $repository = $this->getRepository();
     $location = $repository->getLocationService()->loadLocation($locationId);
     $contentType = $repository->getContentTypeService()->loadContentType($location->getContentInfo()->contentTypeId);
     $response = new Response();
     $response->setTtl(strtotime('1 day'));
     return $this->render('file:' . __DIR__ . '/../ezpublish_legacy/metalfrance/design/mf/templates/parts/right_column/related_content.tpl', ['module_result' => ['content_info' => ['class_identifier' => $contentType->identifier, 'node_id' => $locationId]]], $response);
 }
 /**
  * @param Request $request
  * @return Response
  */
 public function indexAction(Request $request)
 {
     if ($this->isLeapYear($request->attributes->get('year'))) {
         $response = new Response('Yes its leap year');
     } else {
         $response = new Response('No it\'s not leap year ' . rand(1, 1000) . ' ');
     }
     $response->setTtl(10);
     return $response;
 }
 public function indexAction(Request $request, $year)
 {
     $leapyear = new LeapYear();
     if ($leapyear->isLeapYear($year)) {
         $response = new Response('Yep, this is a leap year!');
     } else {
         $response = new Response('Nope, this is not a leap year.');
     }
     $response->setTtl(10);
     return $response;
 }
 /**
  * {@inheritdoc}
  */
 public function alterResponse(Response $response)
 {
     if (!$response->isCacheable()) {
         // the controller flags the response as private so we keep it private!
         return;
     }
     // a block has a lower ttl that the current response, so we update the ttl to match
     // the one provided in the block
     if ($this->currentTtl < $response->getTtl()) {
         $response->setTtl($this->currentTtl);
     }
 }
 /**
  * Renders a view and returns a Response.
  *
  * To stream a view, pass an instance of StreamedResponse as a third argument.
  *
  * @param string   $view       The view name
  * @param array    $parameters An array of parameters to pass to the view
  * @param Response $response   A Response instance
  *
  * @return Response A Response instance
  */
 public function render($view, array $parameters = [], Response $response = null)
 {
     $twig = $this['twig'];
     if ($response instanceof StreamedResponse) {
         $response->setCallback(function () use($twig, $view, $parameters) {
             $twig->display($view, $parameters);
         });
         return $response;
     }
     if (null === $response) {
         $response = new Response();
     }
     if ($this->hasCachingEnabled()) {
         $response->setTtl($this['http_cache.default_ttl']);
     }
     $response->setContent($twig->render($view, $parameters));
     return $response;
 }
Beispiel #8
0
 /**
  * Creates the JSON used to render the stats.
  *
  * @param Request $request
  *
  * @return Response
  */
 protected function statsJson(Request $request)
 {
     $response = new Response();
     $response->setTtl(60 * 5);
     $response->setPublic();
     if ($response->isNotModified($request)) {
         return $response;
     }
     $event = $this->eventRepo->getNextEvent()->getOrThrow(new AccesDeniedHttpException('No event.'));
     $tickets = $this->ticketRepo->getTicketsForEvent($event);
     $unregistrations = $this->unregistrationRepo->getUnregistrationsForEvent($event);
     $stats = array('checkins' => array('sa' => $this->getCheckinsPerDay($tickets, Ticket::DAY_SATURDAY), 'sa_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SATURDAY), 'su' => $this->getCheckinsPerDay($tickets, Ticket::DAY_SUNDAY), 'su_hour' => $this->getCheckinsPerHour($tickets, Ticket::DAY_SUNDAY), 'unique' => array('sa' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SATURDAY), 'su' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY), 'both' => $this->getUniqueDayCheckins($tickets, Ticket::DAY_SUNDAY, true)), 'noshows' => array('sa' => $this->getNoShows($tickets, Ticket::DAY_SATURDAY), 'su' => $this->getNoShows($tickets, Ticket::DAY_SUNDAY))), 'unregistrations' => $this->getUnregistrationsPerDay($unregistrations));
     $data = array('stats' => $stats);
     $response->setContent(json_encode($data));
     $response->setCharset('utf-8');
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 public function testIsCacheableWithSetTtl()
 {
     $response = new Response();
     $response->setTtl(10);
     $this->assertTrue($response->isCacheable());
 }
Beispiel #10
0
 public function fooAction()
 {
     $response = new Response('cached response (10 sec): ' . time());
     $response->setTtl(10);
     return $response;
 }
 /**
  * This method is responsible to cascade ttl to the parent block.
  *
  * @param Response              $response
  * @param BlockContextInterface $blockContext
  * @param BlockServiceInterface $service
  *
  * @return Response
  */
 protected function addMetaInformation(Response $response, BlockContextInterface $blockContext, BlockServiceInterface $service)
 {
     // a response exists, use it
     if ($this->lastResponse && $this->lastResponse->isCacheable()) {
         $response->setTtl($this->lastResponse->getTtl());
         $response->setPublic();
     } elseif ($this->lastResponse) {
         // not cacheable
         $response->setPrivate();
         $response->setTtl(0);
         $response->headers->removeCacheControlDirective('s-maxage');
         $response->headers->removeCacheControlDirective('maxage');
     }
     // no more children available in the stack, reseting the state object
     if (!$blockContext->getBlock()->hasParent()) {
         $this->lastResponse = null;
     } else {
         // contains a parent so storing the response
         $this->lastResponse = $response;
     }
     return $response;
 }
Beispiel #12
0
 public function rssAction($uri = null)
 {
     if (null === $this->application) {
         throw new FrontControllerException('A valid BackBee application is required.', FrontControllerException::INTERNAL_ERROR);
     }
     if (false === $this->application->getContainer()->has('site')) {
         throw new FrontControllerException('A BackBee\\Site instance is required.', FrontControllerException::INTERNAL_ERROR);
     }
     $site = $this->application->getContainer()->get('site');
     if (false !== ($ext = strrpos($uri, '.'))) {
         $uri = substr($uri, 0, $ext);
     }
     if ('_root_' == $uri) {
         $page = $this->application->getEntityManager()->getRepository('BackBee\\NestedNode\\Page')->getRoot($site);
     } else {
         $page = $this->application->getEntityManager()->getRepository('BackBee\\NestedNode\\Page')->findOneBy(array('_site' => $site, '_url' => '/' . $uri, '_state' => Page::getUndeletedStates()));
     }
     try {
         $this->application->info(sprintf('Handling URL request `rss%s`.', $uri));
         $response = new Response($this->application->getRenderer()->render($page, 'rss', null, 'rss.phtml', false));
         $response->headers->set('Content-Type', 'text/xml');
         $response->setClientTtl(15);
         $response->setTtl(15);
         $this->send($response);
     } catch (\Exception $e) {
         $this->defaultAction('/rss/' . $uri);
     }
 }
Beispiel #13
0
 public function messagesAction(Application $app)
 {
     $response = new Response();
     return $app->render('messages.twig', ['guestbook' => $app['guestbook']->get()], $response->setTtl(10));
 }
Beispiel #14
0
        if ($form->isValid()) {
            $app['session']->setFlash('success', 'The form is valid');
        } else {
            $form->addError(new FormError('This is a global error'));
            $app['session']->setFlash('info', 'The form is bind, but not valid');
        }
    }
    return $app['twig']->render('form.html.twig', array('form' => $form->createView()));
})->bind('form');
$app->match('/logout', function () use($app) {
    $app['session']->clear();
    return $app->redirect($app['url_generator']->generate('homepage'));
})->bind('logout');
$app->get('/page-with-cache', function () use($app) {
    $response = new Response($app['twig']->render('page-with-cache.html.twig', array('date' => date('Y-M-d h:i:s'))));
    $response->setTtl(10);
    return $response;
})->bind('page_with_cache');
$app->error(function (\Exception $e, $code) use($app) {
    if ($app['debug']) {
        return;
    }
    switch ($code) {
        case 404:
            $message = 'The requested page could not be found.';
            break;
        default:
            $message = 'We are sorry, but something went terribly wrong.';
    }
    return new Response($message, $code);
});
 public function cache(App $app)
 {
     $response = new Response($app['twig']->render('cache.html.twig', array('date' => date('Y-M-d h:i:s'))));
     $response->setTtl(10);
     return $response;
 }
 public function cacheAction(Application $app, Request $request)
 {
     $response = new Response($app['twig']->render('page-with-cache.html.twig', array('date' => date('Y-M-d h:i:s'))));
     $response->setTtl(10);
     return $response;
 }