跳至內容 跳至搜尋
命名空間
方法
E
F
H
N
S
包含的模組

實體公共方法

expires_in(seconds, options = {})

設定 `Cache-Control` 標頭,覆寫現有指令。這個方法也會確保 HTTP `Date` 標頭供用戶端相容。

預設發出 `private` 指令,讓中間快取不得快取回應。

選項

:public

如果為 true,替換預設 `private` 指令為 `public` 指令。

:must_revalidate

如果為 true,加入 `must-revalidate` 指令。

:stale_while_revalidate

設定 `stale-while-revalidate` 指令的值。

:stale_if_error

設定 `stale-if-error` 指令的值。

:immutable

如果為 true,加入 `immutable` 指令。

任何額外的金鑰值對都會連結成指令。支援的 `Cache-Control` 指令列表,請參閱 MDN 上的文章

範例

expires_in 10.minutes
# => Cache-Control: max-age=600, private

expires_in 10.minutes, public: true
# => Cache-Control: max-age=600, public

expires_in 10.minutes, public: true, must_revalidate: true
# => Cache-Control: max-age=600, public, must-revalidate

expires_in 1.hour, stale_while_revalidate: 60.seconds
# => Cache-Control: max-age=3600, private, stale-while-revalidate=60

expires_in 1.hour, stale_if_error: 5.minutes
# => Cache-Control: max-age=3600, private, stale-if-error=300

expires_in 1.hour, public: true, "s-maxage": 3.hours, "no-transform": true
# => Cache-Control: max-age=3600, public, s-maxage=10800, no-transform=true
# File actionpack/lib/action_controller/metal/conditional_get.rb, line 290
def expires_in(seconds, options = {})
  response.cache_control.delete(:no_store)
  response.cache_control.merge!(
    max_age: seconds,
    public: options.delete(:public),
    must_revalidate: options.delete(:must_revalidate),
    stale_while_revalidate: options.delete(:stale_while_revalidate),
    stale_if_error: options.delete(:stale_if_error),
    immutable: options.delete(:immutable),
  )
  options.delete(:private)

  response.cache_control[:extras] = options.map { |k, v| "#{k}=#{v}" }
  response.date = Time.now unless response.date?
end

expires_now()

設定 HTTP 1.1 `Cache-Control` 設定為 `no-cache`。這表示資源會被標記為過期,所以用戶端必須重新驗證。伺服器/瀏覽器快取仍可能儲存該資源。

# File actionpack/lib/action_controller/metal/conditional_get.rb, line 309
def expires_now
  response.cache_control.replace(no_cache: true)
end

fresh_when(object = nil, etag: nil, weak_etag: nil, strong_etag: nil, last_modified: nil, public: false, cache_control: {}, template: nil)

設定回應上的 `etag`、`last_modified` 或兩個,如果要求已經過期則產生 `304 Not Modified` 驗證。

選項

:etag

在回應上設定「弱」ETag 驗證。請參閱 `:weak_etag` 選項。

:weak_etag

在回應上設定「弱」ETag 驗證。指定 `If-None-Match` 標頭的要求可能會接收 `304 Not Modified` 驗證,如果 ETag 完全符合。

弱 ETag 指示語意相等,而非位元相等,所以它們適合在瀏覽器快取中快取 HTML 頁面。它們不能用於必須位元相符的回應,例如在 PDF 檔中傳輸 `Range` 要求。

:strong_etag

在回應中設定一個「強」ETag驗證器。如果ETag完全相符,指定If-None-Match標頭的請求可能會收到304 Not Modified回應。

一個強ETag意味著完全相等 - 回應必須逐位元組相符。這對於服務大型視訊或PDF檔案中的Range請求是必須的,例如,或者對於不支援弱ETag的一些CDN相容性是必須的。

:last_modified

在回應中設定一個「弱」最後更新驗證器。如果last_modified <= If-Modified-Since,後續指定If-Modified-Since標頭的請求可能會收到304 Not Modified回應。

:public

預設Cache-Control標頭是私密的。如果您想要您的應用程式可以被其他裝置快取,例如代理快取,請將此選項設為true

:cache_control

給定時,會覆寫現有的Cache-Control標頭。有關Cache-Control指令清單,請參閱MDN上的文章

:template

預設情況下,目前控制項/動作的模板摘要包含在ETag中。如果動作呈現不同的模板,您可以包含其摘要。如果動作根本不呈現模板,您可以傳遞template: false以略過任何檢查模板摘要的嘗試。

範例

def show
  @article = Article.find(params[:id])
  fresh_when(etag: @article, last_modified: @article.updated_at, public: true)
end

如果請求指定匹配的ETag和If-Modified-Since標頭,這將會傳送一個304 Not Modified回應。否則,它會呈現show模板。

您也可以只傳遞一個記錄

def show
  @article = Article.find(params[:id])
  fresh_when(@article)
end

etag將被設為記錄,而last_modified將被設為記錄的updated_at

您也可以傳遞一個回應maximum的物件,例如記錄集合

def index
  @articles = Article.all
  fresh_when(@articles)
end

在這種情況下,etag將被設為集合,而last_modified將被設為maximum(:updated_at)(最近更新的記錄的時間戳記)。

傳遞記錄或集合時,您仍然可以指定其他選項,例如:public:cache_control

def show
  @article = Article.find(params[:id])
  fresh_when(@article, public: true, cache_control: { no_cache: true })
end

以上將在回應中設定Cache-Control: public, no-cache

當呈現與控制項/動作預設模板不同的模板時,您可以指出要在ETag中包含哪些摘要

before_action { fresh_when @article, template: "widgets/show" }
# File actionpack/lib/action_controller/metal/conditional_get.rb, line 137
def fresh_when(object = nil, etag: nil, weak_etag: nil, strong_etag: nil, last_modified: nil, public: false, cache_control: {}, template: nil)
  response.cache_control.delete(:no_store)
  weak_etag ||= etag || object unless strong_etag
  last_modified ||= object.try(:updated_at) || object.try(:maximum, :updated_at)

  if strong_etag
    response.strong_etag = combine_etags strong_etag,
      last_modified: last_modified, public: public, template: template
  elsif weak_etag || template
    response.weak_etag = combine_etags weak_etag,
      last_modified: last_modified, public: public, template: template
  end

  response.last_modified = last_modified if last_modified
  response.cache_control[:public] = true if public
  response.cache_control.merge!(cache_control)

  head :not_modified if request.fresh?(response)
end

http_cache_forever(public: false)

快取或產生區塊。快取應永遠不會過期。

當您有永遠不會變更的HTTP回應時,可以使用此方法,而瀏覽器和代理程式應無限期地快取它。

  • public:預設情況下,HTTP回應是私密的,僅快取在使用者的網路瀏覽器上。為允許代理快取回應,請設定true以表示它們可以對所有使用者提供快取的回應。

# File actionpack/lib/action_controller/metal/conditional_get.rb, line 321
def http_cache_forever(public: false)
  expires_in 100.years, public: public, immutable: true

  yield if stale?(etag: request.fullpath,
                  last_modified: Time.new(2011, 1, 1).utc,
                  public: public)
end

no_store()

設定HTTP 1.1 Cache-Control標頭為no-store。這表示資源可能無法儲存在任何快取中。

# File actionpack/lib/action_controller/metal/conditional_get.rb, line 331
def no_store
  response.cache_control.replace(no_store: true)
end

stale?(object = nil, **freshness_kwargs)

設定回應中的 etag 和/或 last_modified 並根據要求進行檢查。如果要求不符合提供的選項,則視為過期,且回應應從頭開始呈現。否則,就是最新的,則會傳送 304 未修改

選項

請參閱 fresh_when 以取得支援的選項。

範例

def show
  @article = Article.find(params[:id])

  if stale?(etag: @article, last_modified: @article.updated_at)
    @statistics = @article.really_expensive_call
    respond_to do |format|
      # all the supported formats
    end
  end
end

您也可以只傳遞一個記錄

def show
  @article = Article.find(params[:id])

  if stale?(@article)
    @statistics = @article.really_expensive_call
    respond_to do |format|
      # all the supported formats
    end
  end
end

etag將被設為記錄,而last_modified將被設為記錄的updated_at

您也可以傳遞一個回應maximum的物件,例如記錄集合

def index
  @articles = Article.all

  if stale?(@articles)
    @statistics = @articles.really_expensive_call
    respond_to do |format|
      # all the supported formats
    end
  end
end

在這種情況下,etag將被設為集合,而last_modified將被設為maximum(:updated_at)(最近更新的記錄的時間戳記)。

傳遞記錄或集合時,您仍然可以指定其他選項,例如:public:cache_control

def show
  @article = Article.find(params[:id])

  if stale?(@article, public: true, cache_control: { no_cache: true })
    @statistics = @articles.really_expensive_call
    respond_to do |format|
      # all the supported formats
    end
  end
end

以上將在回應中設定Cache-Control: public, no-cache

當呈現與控制項/動作預設模板不同的模板時,您可以指出要在ETag中包含哪些摘要

def show
  super if stale?(@article, template: "widgets/show")
end
# File actionpack/lib/action_controller/metal/conditional_get.rb, line 236
def stale?(object = nil, **freshness_kwargs)
  fresh_when(object, **freshness_kwargs)
  !request.fresh?(response)
end