'Symfony full page in Redis cach

I know we can store items from db in Redis, but it is possible to store rendered full page in Redis cache?



Solution 1:[1]

Yes, I think so. You can render the page in a controller/service and pass it to Redis, there's a documentation snippet that shows how to render a Twig template and save the results in a variable:

// src/Service/SomeService.php
namespace App\Service;

use Twig\Environment;

class SomeService
{
    private $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    public function someMethod()
    {
        // ...

        $htmlContents = $this->twig->render('product/index.html.twig', [
            'category' => '...',
            'promotions' => ['...', '...'],
        ]);
    }
}

You can then use Cache Contracts (assuming you have configured Redis Adapter) to get/set rendered page from cache.

If you would wish to render it, you might want to return retrieved page in Response object in desired controller.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Mi?ka