设置缓存控制标头

时间:2018-04-23 19:35:41

标签: ruby-on-rails ruby-on-rails-5

我们说我有一个public/marketing控制器,我想用Cache-Control: max-age=180, public, must-revalidate设置响应标头

我无法找到在控制器级别设置的文档吗?

1 个答案:

答案 0 :(得分:1)

有几个选项可供考虑。

选项1:

使用来自ActionController::ConditionalGet的expires_in助手。这些都包含在ActionController::BaseActionController::API中,据我记得(http://api.rubyonrails.org/classes/ActionController/ConditionalGet.html)。

def some_action
  @some_user_for_view = User.first
  expires_in 3.hours, public: true
end

选项2:

在响应对象上设置#headers,手动设置标头。直。 (http://edgeguides.rubyonrails.org/action_controller_overview.html#the-response-object

before_action :set_headers

def set_headers
  response.headers["Expires"]='Mon, 01 Jan 2000 00:00:00 GMT'
  response.headers["Pragma"]='no-cache'
  response.headers["Cache-Control"]="no-cache"
  response.headers["Last-Modified"]=Time.now.strftime("%a, %d %b %Y %T %Z")
end

第一个选项是最常用的'但是使用第二个选项会在自定义标题方面产生更多选项。

相关问题