Caching 从Smarty 3到Twig 2,如何管理模板的出现?

Caching 从Smarty 3到Twig 2,如何管理模板的出现?,caching,twig,smarty,Caching,Twig,Smarty,我在几个网站上使用Smarty 3,我正在回顾twig的工作原理。 与Smarty相比,Twig有一点我不明白,即如何管理同一模板的出现 在Smarty中,首先使用模板名称和该模板特定页面的缓存ID创建模板缓存 $template = $smarty->createTemplate('article.tpl', 'article|12'); 这将创建一个缓存页面 要在Smarty中呈现结果,您首先测试页面是否存在于缓存中,然后呈现页面,然后访问数据库以检索数据并分配变量(&D) if (

我在几个网站上使用Smarty 3,我正在回顾twig的工作原理。 与Smarty相比,Twig有一点我不明白,即如何管理同一模板的出现

在Smarty中,首先使用模板名称和该模板特定页面的缓存ID创建模板缓存

$template = $smarty->createTemplate('article.tpl', 'article|12');
这将创建一个缓存页面

要在Smarty中呈现结果,您首先测试页面是否存在于缓存中,然后呈现页面,然后访问数据库以检索数据并分配变量(&D)

if (!$template->isCached()) {
   // access to the database
   // assign variables
}
然后呈现页面

$tpl->display();
但在树枝上我只看到了

$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, ['cache' => 
    '/path/to/compilation_cache']);
$template = $twig->load('index.html');
echo $template->render(['the' => 'variables', 'go' => 'here']);

什么是“!$template->isCached()”的等价物以避免每次访问数据库?

没有。Twig不会自行缓存呈现的HTML输出。它只将模板编译的结果缓存到PHP代码中,每次呈现模板时仍会执行PHP代码

在应用程序前面需要一个反向代理,或者某种数据库缓存层,以避免重复调用应用程序