php Symfony2:禁用 Twig 缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18883546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:24:01  来源:igfitidea点击:

Symfony2: disable Twig cache

phpcachingsymfonypdftwig

提问by sylzys

I'm trying to disable twig cache in prod mode, or to force it to recompile my views.

我试图在 prod 模式下禁用树枝缓存,或者强制它重新编译我的视图。

I'm using KnapLaps SnappyBundle to generate some PDFs (the same problem appears with DomPDF), and I have dynamic content to render.

我正在使用 KnapLaps SnappyBundle 生成一些 PDF(DomPDF 出现同样的问题),并且我有动态内容要呈现。

When in dev mode, I can modify some text, or even some css properties, the changes are effective immediately.

在开发模式下,我可以修改一些文本,甚至一些 css 属性,这些更改立即生效。

But in prod mode, I need to cache:clear, or to rm -rf app/cache/prod/twig/* to see the changes.

但是在 prod 模式下,我需要缓存:清除,或者到 rm -rf app/cache/prod/twig/* 来查看更改。

I tried the following options in my config.yml for Twig section (not at the same time)

我在 Twig 部分的 config.yml 中尝试了以下选项(不是同时)

cache: "/dev/null"
cache: false
auto-reload: ~

I also try some stuff with header when generating and redering my pdf:

在生成和重新生成我的 pdf 时,我还尝试了一些带有标题的内容:

$html = $this->renderView("xxxxPdfBundle:Pdf:test.html.twig", array("foo" => $bar));
return new Response(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
    200,
    array(
        'Cache-Control'         => 'no-cache, must-revalidate, post-check=0, pre-check=0',
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename='.$file
    )
);

I can't figure out how to force twig to recompile or not use the app/cache, because obviously the pdf content will be dynamic when in production.

我不知道如何强制 twig 重新编译或不使用应用程序/缓存,因为显然 pdf 内容在生产中是动态的。

Info update from the comments:

来自评论的信息更新:

I perceived that even the dynamic template variables were not updated, so the same PDF got generated over and over again in production, but not in development.

我发现即使是动态模板变量也没有更新,因此在生产中一遍又一遍地生成相同的 PDF,但在开发中却没有。

After clearing all caches again, that issue is fixed: PDFs are now generated with dynamic content as designed.

再次清除所有缓存后,该问题得到解决:现在生成的 PDF 具有设计的动态内容。

Still, a question remains: what if, when my website is in production, I decide to change the CSS styling inside a pdf template ? CSS is not template variable, and I can't force people to empty their cache :/

尽管如此,一个问题仍然存在:如果我的网站在生产中,我决定更改 pdf 模板中的 CSS 样式怎么办?CSS 不是模板变量,我不能强迫人们清空他们的缓存:/

采纳答案by Sven

The question of client-side caching has some answers.

客户端缓存的问题有一些答案。

First, HTTP employs some headers that describe to the client how to do the caching. The worst of them is to declare that the received resource should be considered cacheable for the next time X without revalidating for updates. The less intrusive version is to add a header with a signature of the delivered version or a last-modified timestamp, and the client should revalidate every time whether or not the resource is still up to date, before using it.

首先,HTTP 使用一些标头向客户端描述如何进行缓存。其中最糟糕的是声明接收到的资源应该在下一次 X 时被认为是可缓存的,而无需重新验证更新。侵入性较小的版本是添加带有交付版本签名或上次修改时间戳的标头,并且客户端应在每次使用之前重新验证资源是否仍然是最新的。

The first kind of caching can only be updated by deleting the client cache in the browser. The second could probably be circumvented by force-loading the page again (Ctrl-F5 or so), but this really is as hidden as the menu allowing to clear the cache.

第一种缓存只能通过删除浏览器中的客户端缓存来更新。第二个可能可以通过再次强制加载页面(Ctrl-F5 左右)来规避,但这确实与允许清除缓存的菜单一样隐藏。

To play it safe, the usual approach would be to add a tag, revision number, incremented counter or whatever is available, to the query string of the URL used for that resource.

为了安全起见,通常的方法是向用于该资源的 URL 的查询字符串添加标签、修订号、递增计数器或任何可用的内容。

  1. http://example.com/generated/summary.pdf?v=1234
  2. http://example.com/generated/summary.pdf?v=1235
  1. http://example.com/generated/summary.pdf?v=1234
  2. http://example.com/generated/summary.pdf?v=1235

The first URL is from deployment run 1234, the second is from 1235 - this number changes the URL enough to trigger a new request instead of getting the old version from the cache.

第一个 URL 来自部署运行 1234,第二个来自 1235 - 这个数字改变了 URL 足以触发新请求,而不是从缓存中获取旧版本。

I don't know if there is something available in your system to act like this. You could also always add an ever changing value like the current timestamp to avoid caching at all, if you cannot disable the HTTP caching headers.

我不知道您的系统中是否有可用的东西来执行此操作。如果您无法禁用 HTTP 缓存标头,您还可以始终添加一个不断变化的值(如当前时间戳)以避免缓存。

回答by flu

The correct way to disable Twig's caching mechanism is to set the cacheenvironment parameter to falseinstead of a cache directory:

禁用 Twig 缓存机制的正确方法是将cache环境参数设置为false而不是缓存目录:

# config_dev.yml
# ...
twig:
    cache: false

References:

参考:

Twig Environment Options

树枝环境选项

TwigBundle Configuration

TwigBundle 配置