跳至内容 跳至搜索
方法
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"
 
MUST_UNDERSTAND = "must-understand"
 
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 must-understand])
 

Attributes

[R] cache_control

实例公共方法

date()

# File actionpack/lib/action_dispatch/http/cache.rb, line 193
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 203
def date=(utc_time)
  set_header DATE, utc_time.httpdate
end

date?()

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

etag?()

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

last_modified()

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

last_modified?()

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

strong_etag=(strong_validators)

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

weak_etag=(weak_validators)

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