How to enable cache in symfony

Caching with a Gateway Cache
To enable caching, modify the code of a front controller to use the caching kernel:

// web/app.php
require_once __DIR__.’/../app/bootstrap.php.cache’;
require_once __DIR__.’/../app/AppKernel.php’;
require_once __DIR__.’/../app/AppCache.php’;

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel(‘prod’, false);
$kernel->loadClassCache();
// wrap the default AppKernel with the AppCache one
$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
The cache kernel has a special getLog() method that returns a string representation of what happened in the cache layer. In the development environment, use it to debug and validate your cache strategy:

error_log($kernel->getLog());
Introduction to HTTP Caching
HTTP specifies four response cache headers that we’re concerned with:

Cache-Control

Last-Modified
The Cache-Control Header
Expires
ETag

The Cache-Control header is unique in that it contains not one, but various pieces of information about the cacheability of a response. Each piece of information is separated by a comma:

Cache-Control: private, max-age=0, must-revalidate
Cache-Control: max-age=7200, must-revalidate
Symfony provides abstraction layer:

$response = new Response();

// mark the response as either public or private
$response->setPublic();
$response->setPrivate();

// set the private or shared max age
$response->setMaxAge(800);
$response->setSharedMaxAge(800);

// set a custom Cache-Control directive
$response->headers->addCacheControlDirective(‘must-revalidate’, true);