禁用特定模板的 Twig 编译缓存 [英] Disable Twig compilation cache for a particular template
问题描述
有没有办法禁用特定模板的 Twig 编译缓存?
Is there a way to disable the Twig compilation cache for a particular template?
我的电子邮件模板使用 Twig.当有人更新这些模板时,它们会被写入磁盘.虽然在生产中,我应该清除整个缓存,否则不会注意到更新.
I'm using Twig for my email templates. When someone updates these templates, they are written to disk. Although in production, I should clear the entire cache otherwise the updates aren't noticed.
这就是为什么我要禁用这些特定模板的 Twig 缓存.我不介意额外的处理能力,因为清除我的整个缓存会对性能造成更大的影响.
That's why I want to disable the Twig cache for these particular templates. I don't mind the extra processing power, as clearing my entire cache is a bigger performance hit.
推荐答案
我认为您的答案可能不是禁用特定模板的缓存,而是在模板更新后清除缓存.我没有测试下面的代码,但它似乎是合理的.稍微玩一下
I think your answer might be not in disabling cache for a specific template, but clearing the cache for a template after it's been updated. I haven't tested the code below, but it seems reasonable. Play around with it a bit
在您保存模板的操作/服务中(在保存模板后):
In your action/service that saves a template (after the template has been saved):
$fileCache = $this->container->get('twig')->getCacheFilename('AcmeDemoBundle:Default:index.html.twig');
if (is_file($fileCache)) {
@unlink($fileCache);
}
有关更多信息,请查看如何在/vendor/twig/twig/lib/Twig/Environment.php (\Twig_Environment) -- 方法 loadTemplate() 中提交 twig 缓存文件.
For more information, check out how twig cache files are handed in /vendor/twig/twig/lib/Twig/Environment.php (\Twig_Environment) -- method loadTemplate().
这篇关于禁用特定模板的 Twig 编译缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!