簡體   English   中英

設置緩存控制標頭

[英]Set Cache-Control Header

假設我有一個public/marketing控制器,我想用Cache-Control: max-age=180, public, must-revalidate設置響應頭Cache-Control: max-age=180, public, must-revalidate

我找不到在控制器級別設置的文檔?

有幾種選擇可以想到。

選項1:

使用來自ActionController::ConditionalGet expires_in助手。 據我記得,這些都包含在ActionController::BaseActionController::APIhttp://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

第一個選項是最“有軌”的,但是使用第二個選項會在自定義標題方面產生更多選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM