跳至内容 跳至搜索
方法
D
E
L
S
W

常量

DATE = "Date"
 
DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
 
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 68
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 78
def date=(utc_time)
  set_header DATE, utc_time.httpdate
end

date?()

# File actionpack/lib/action_dispatch/http/cache.rb, line 74
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 101
def etag=(weak_validators)
  self.weak_etag = weak_validators
end

etag?()

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

last_modified()

# File actionpack/lib/action_dispatch/http/cache.rb, line 54
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 64
def last_modified=(utc_time)
  set_header LAST_MODIFIED, utc_time.httpdate
end

last_modified?()

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

strong_etag=(strong_validators)

# File actionpack/lib/action_dispatch/http/cache.rb, line 109
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 121
def strong_etag?
  etag? && !weak_etag?
end

weak_etag=(weak_validators)

# File actionpack/lib/action_dispatch/http/cache.rb, line 105
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 116
def weak_etag?
  etag? && etag.start_with?('W/"')
end