1

When doing HTTP caching in rails, once would use:
fresh_when etag: @user, public: true

The problem with that is that if you chance your html for the page, the cached page is no longer going to be valid. Therefor we need a way to invalidate the page/etag every time we change something.

One way to do that is to change RAILS_CACHE_ID every time you deploy as per Etag busting on deployment .However doing that will results in all etags, for all the pages getting expired for every deployment.

I would rather have a solution where I can expire individual tags, for individual pages.

I could generate my own etag with a version like this: fresh_when etag: (@user.updated_at.to_s + "V1"), public: true

But this just doesn't feel like the "Rails way". Any more elegant solutions?

4

2 回答 2

1

本文更新了相应的库,因此您只需在 config.xml 中设置一个版本即可。请参阅https://github.com/n8/bust_rails_etags上的自述文件

于 2013-01-15T08:29:02.707 回答
0

如果您使用的是 Capistrano,或者Rails.root每次部署时值都发生变化的部署策略,您可以将其添加到您的应用程序控制器中:

class ApplicationController < ActionController::Base

  # This causes http request caching to expire when we deploy.
  # The app is deployed /mnt/code/releases/<timestamp>
  etag { $etag_cache_control_fragement ||= Rails.root.to_s.split("/").last }

参考这里:http ://api.rubyonrails.org/v5.1/classes/ActionController/ConditionalGet/ClassMethods.html

于 2017-11-16T20:20:32.530 回答