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);
// wrap the default AppKernel with the AppCache one
$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$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:

Introduction to HTTP Caching
HTTP specifies four response cache headers that we’re concerned with:


The Cache-Control Header

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

// set the private or shared max age

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