Redis cach中的Symfony完整页面



我知道我们可以在Redis中存储来自db的项目,但是否可以将渲染的完整页面存储在Redis缓存中?

是的,我想是的。你可以在控制器/服务中呈现页面并将其传递给Redis,有一个文档片段显示了如何呈现Twig模板并将结果保存在变量中:

// src/Service/SomeService.php
namespace AppService;
use TwigEnvironment;
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' => ['...', '...'],
]);
}
}

然后,您可以使用缓存合约(假设您已配置Redis Adapter(从缓存中获取/设置渲染页面。

若要渲染它,可能需要在所需控制器中的Response对象中返回检索到的页面。

最新更新