[英]Set Cache-Control Header
假設我有一個public/marketing
控制器,我想用Cache-Control: max-age=180, public, must-revalidate
設置響應頭Cache-Control: max-age=180, public, must-revalidate
我找不到在控制器級別設置的文檔?
有幾種選擇可以想到。
使用來自ActionController::ConditionalGet
expires_in助手。 據我記得,這些都包含在ActionController::Base
和ActionController::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
通過在響應對象上設置#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
第一個選項是最“有軌”的,但是使用第二個選項會在自定義標題方面產生更多選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.