Rails - etags 与页面缓存(文件缓存)
使用 etags/stale?/fresh_when 有哪些优点? 而不是页面缓存(在文件缓存上)?
Apache 自动处理静态文件的 etag,但即使不这样做,页面缓存仍然会更好,因为 Rails 应用程序甚至不会被调用。
那么,在什么情况下我会使用 Rails 提供的方法(stale?/fresh_when?)?
What would be some advantages of using etags/stale?/fresh_when? instead of page caching (on a file cache)?
Apache automatically handles etags for static files, but even if it didn't, page caching would still be better since the Rails app doesn't even get called.
So, in what instances would I use the methods provided by Rails (stale?/fresh_when?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们真的很互补。 Etags/fresh_when 等可以帮助您很好地处理下游缓存(例如您自己的 Varnish/Squid 实例或 Rack::Cache 或浏览器缓存或 ISP 代理服务器...)
页面缓存可以完全避免您碰到 Rails 堆栈,因为 Apache/您的 Web 服务器提供文件,因此不会进行数据库查找。 但是您必须处理缓存过期问题以保持缓存最新。
使用 etags/conditional get,您不会节省太多处理时间,因为您仍然需要获取页面上使用的所有记录:
在用户有当前页面的情况下,它可以节省一些渲染时间和所需的带宽发送页面。
They are really complimentary. Etags/fresh_when etc. help you play nice with downstream caches (like your own Varnish/Squid instances or Rack::Cache or the Browser cache or ISP Proxy Servers…)
Page caching saves you from hitting your rails stack entirely because Apache/your webserver serve the file, so no DB lookups are done. But you have to deal with cache expiration to keep the cache fresh.
Using etags/conditional get, you don't save much processing time since you still need to to get all the records used on the page:
in the case that the user has a current page, it saves you some rendering time and the bandwidth required to send down the page.
我想到的另一个用途是,在让 Rails 分发“304 Not Modified”标头之前,您仍然可以处理一些信息。 就像您想记录页面点击一样。
Another use that occurred to me was that you could still process some information before letting Rails hand out the "304 Not Modified" header. Like if you want to record hits to a page.
我想到的一件事是,即使您清除了整个页面缓存,
fresh_when
仍然会为您节省一些渲染时间。 在这里,您将同时使用两者。我也对其他答案感到好奇。
One thing that comes to mind is that
fresh_when
will still save you some rendering even if you cleared the entire page cache. Here you'd be using both in tandem.I'm curious about other answers as well.