跳至內容 跳至搜尋
方法
D
E
L
S
W

常數

DATE = "Date"
 
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
 
IMMUTABLE = "immutable"
 
LAST_MODIFIED = "Last-Modified"
 
MUST_REVALIDATE = "must-revalidate"
 
NO_CACHE = "no-cache"
 
NO_STORE = "no-store"
 
PRIVATE = "private"
 
PUBLIC = "public"
 
SPECIAL_KEYS = Set.new(%w[extras no-store no-cache max-age public private must-revalidate])
 

屬性

[R] cache_control

實體公開方法

date()

# File actionpack/lib/action_dispatch/http/cache.rb, line 85
def date
  if date_header = get_header(DATE)
    Time.httpdate(date_header)
  end
end

date=(utc_time)

# File actionpack/lib/action_dispatch/http/cache.rb, line 95
def date=(utc_time)
  set_header DATE, utc_time.httpdate
end

date?()

# File actionpack/lib/action_dispatch/http/cache.rb, line 91
def date?
  has_header? DATE
end

etag=(weak_validators)

此方法在響應上設置一個弱 ETag 驗證器,以便瀏覽器和代理可以快取響應,並以 ETag 作為鍵值。在後續請求中,`If-None-Match` 標頭會設置為快取的 ETag。如果它與目前的 ETag 匹配,我們可以返回一個沒有內容的 `304 Not Modified` 響應,讓瀏覽器或代理知道他們的快取是最新的。這可以大幅節省請求時間和網路頻寬。

弱 ETag 被認為在語義上等效,但並非逐位元組相同。這非常適合瀏覽器快取 HTML 頁面,我們不關心完全相同,只關心使用者正在查看的內容。

強 ETag 被認為是逐位元組相同的。它們允許瀏覽器或代理快取支援 `Range` 請求,這對於翻頁 PDF 檔案或瀏覽影片很有用。某些 CDN 只支援強 ETag,並且會完全忽略弱 ETag。

弱 ETag 幾乎是我們一直需要的,所以它們是預設的。查看

strong_etag= 以提供強 ETag 驗證器。

# File actionpack/lib/action_dispatch/http/cache.rb, line 117
def etag=(weak_validators)
  self.weak_etag = weak_validators
end

etag?()

# File actionpack/lib/action_dispatch/http/cache.rb, line 129
def etag?; etag; end

last_modified()

# File actionpack/lib/action_dispatch/http/cache.rb, line 71
def last_modified
  if last = get_header(LAST_MODIFIED)
    Time.httpdate(last)
  end
end

last_modified=(utc_time)

# File actionpack/lib/action_dispatch/http/cache.rb, line 81
def last_modified=(utc_time)
  set_header LAST_MODIFIED, utc_time.httpdate
end

last_modified?()

# File actionpack/lib/action_dispatch/http/cache.rb, line 77
def last_modified?
  has_header? LAST_MODIFIED
end

strong_etag=(strong_validators)

# File actionpack/lib/action_dispatch/http/cache.rb, line 125
def strong_etag=(strong_validators)
  set_header "ETag", generate_strong_etag(strong_validators)
end

strong_etag?()

如果設置了 ETag,且它不是弱驗證器(前面沒有 `W/`),則為 True。

# File actionpack/lib/action_dispatch/http/cache.rb, line 138
def strong_etag?
  etag? && !weak_etag?
end

weak_etag=(weak_validators)

# File actionpack/lib/action_dispatch/http/cache.rb, line 121
def weak_etag=(weak_validators)
  set_header "ETag", generate_weak_etag(weak_validators)
end

weak_etag?()

如果設置了 ETag,且它是弱驗證器(前面有 `W/`),則為 True。

# File actionpack/lib/action_dispatch/http/cache.rb, line 132
def weak_etag?
  etag? && etag.start_with?('W/"')
end