Add caching to custom controller

Overview

In this guide you'll learn how to define a controller route as cacheable for the http cache.

Prerequisites

In order to add a cache to an own controller route, you first need a plugin with a controller. Therefore, you can refer to the Add custom controller guide.

Define the controller as cacheable

To define a controller route as cacheable, it must be annotated with @HttpCache(). Once this annotation is set, the core takes care of everything else. If the route is called several times in the same state, a response is generated only for the first request and the second request gets the same response as the first one. It is also possible to exclude certain states from the cache. Shopware sets two different user states to which the HTTP cache reacts:
  • state: logged-in - means that the user is logged in.
  • state: cart-filled - means that there are products in the shopping cart.
If the controller route is not to be cached for one or both of these states, the annotation can be defined as follows: @HttpCache(states={"cart-filled", "logged-in"})
<plugin root>/src/Storefront/Controller/ExampleController.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Storefront\Controller;
4
5
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
6
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
7
use Shopware\Core\System\SalesChannel\SalesChannelContext;
8
use Shopware\Storefront\Controller\StorefrontController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
* @Route(defaults={"_routeScope"={"storefront"}})
15
*/
16
class ExampleController extends StorefrontController
17
{
18
/**
19
* @HttpCache()
20
* @Route("/example", name="frontend.example.example", methods={"GET"})
21
*/
22
public function showExample(): Response
23
{
24
return $this->renderStorefront('@SwagBasicExample/storefront/page/example/index.html.twig', [
25
'example' => 'Hello world'
26
]);
27
}
28
}
Copied!

Cache invalidation

As soon as a controller route has been defined as cacheable, and the corresponding response is written to the cache, it is tagged accordingly. For this purpose, the core uses all cache tags generated during the request or loaded from existing cache entries. The cache invalidation of the storefront controller routes is controlled by the cache invalidation of the store API routes.
For more information about Store API cache invalidation, you can refer to the Add Cache for Store Api Route Guide.
This is because all data loaded in a controller route, is loaded in the core via the corresponding Store API routes and provided with corresponding cache tags. So the tags of the http cache entries we have in the core consists of the sum of all store api tags generated or loaded during the request. Therefore the invalidation of a controller route that loads all data via the store API, no additional invalidation needs to be written.