class Rack::AcornCache::CachedResponse
Constants
- DEFAULT_MAX_AGE
Attributes
body[R]
date[R]
headers[R]
status[R]
Public Class Methods
new(args={})
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 12 def initialize(args={}) @body = args["body"] @status = args["status"] @headers = args["headers"] @cache_control_header = CacheControlHeader.new(headers["Cache-Control"]) end
Public Instance Methods
add_acorn_cache_header!()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 49 def add_acorn_cache_header! unless headers["X-Acorn-Cache"] headers["X-Acorn-Cache"] = "HIT" end self end
etag_header()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 35 def etag_header headers["ETag"] end
expiration_date()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 80 def expiration_date if s_maxage date + s_maxage elsif max_age date + max_age elsif expiration_header expiration else date + DEFAULT_MAX_AGE end end
fresh?()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 72 def fresh? expiration_date > Time.now end
fresh_for_request?(request)
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 96 def fresh_for_request?(request) FreshnessRules.cached_response_fresh_for_request?(self, request) end
last_modified_header()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 39 def last_modified_header headers["Last-Modified"] end
matches?(server_response)
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 56 def matches?(server_response) if etag_header server_response.etag_header == etag_header elsif last_modified_header server_response.last_modified_header == last_modified_header else false end end
must_be_revalidated?()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 19 def must_be_revalidated? no_cache? || must_revalidate? end
serialize()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 27 def serialize { headers: headers, status: status, body: body }.to_json end
time_to_live()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 66 def time_to_live s_maxage || max_age || (expiration_date - date) end
Also aliased as: stale_time_specified?
time_until_expiration()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 92 def time_until_expiration Time.now - expiration end
to_a()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 31 def to_a [status, headers, [body]] end
update_date!()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 23 def update_date! headers["Date"] = Time.now.httpdate end
update_date_and_recache!(cache_key)
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 43 def update_date_and_recache!(cache_key) cached_response.update_date! CacheWriter.write(cache_key, cached_response.serialize) self end
Private Instance Methods
date_header()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 114 def date_header headers["Date"] end
expiration()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 110 def expiration @expiration ||= Time.httpdate(expiration_header) end
expiration_header()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 106 def expiration_header @expiration_header ||= headers["Expiration"] end
expiration_header_time()
click to toggle source
# File lib/acorn_cache/cached_response.rb, line 102 def expiration_header_time Time.httpdate(expiration_header) end