Class: Rack::AcornCache::CachedResponse
- Inherits:
-
Object
- Object
- Rack::AcornCache::CachedResponse
- Extended by:
- Forwardable
- Defined in:
- lib/acorn_cache/cached_response.rb
Constant Summary collapse
- DEFAULT_MAX_AGE =
3600
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #add_acorn_cache_header! ⇒ Object
- #etag_header ⇒ Object
- #expiration_date ⇒ Object
- #fresh? ⇒ Boolean
- #fresh_for_request?(request) ⇒ Boolean
-
#initialize(args = {}) ⇒ CachedResponse
constructor
A new instance of CachedResponse.
- #last_modified_header ⇒ Object
- #matches?(server_response) ⇒ Boolean
- #must_be_revalidated? ⇒ Boolean
- #serialize ⇒ Object
- #time_to_live ⇒ Object (also: #stale_time_specified?)
- #time_until_expiration ⇒ Object
- #to_a ⇒ Object
- #update_date! ⇒ Object
- #update_date_and_recache!(cache_key) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ CachedResponse
Returns a new instance of CachedResponse.
12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/acorn_cache/cached_response.rb', line 9 def body @body end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
9 10 11 |
# File 'lib/acorn_cache/cached_response.rb', line 9 def date @date end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/acorn_cache/cached_response.rb', line 9 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/acorn_cache/cached_response.rb', line 9 def status @status end |
Instance Method Details
#add_acorn_cache_header! ⇒ Object
49 50 51 52 53 54 |
# 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 ⇒ Object
35 36 37 |
# File 'lib/acorn_cache/cached_response.rb', line 35 def etag_header headers["ETag"] end |
#expiration_date ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# 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? ⇒ Boolean
72 73 74 |
# File 'lib/acorn_cache/cached_response.rb', line 72 def fresh? expiration_date > Time.now end |
#fresh_for_request?(request) ⇒ Boolean
96 97 98 |
# 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 ⇒ Object
39 40 41 |
# File 'lib/acorn_cache/cached_response.rb', line 39 def last_modified_header headers["Last-Modified"] end |
#matches?(server_response) ⇒ Boolean
56 57 58 59 60 61 62 63 64 |
# 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? ⇒ Boolean
19 20 21 |
# File 'lib/acorn_cache/cached_response.rb', line 19 def must_be_revalidated? no_cache? || must_revalidate? end |
#serialize ⇒ Object
27 28 29 |
# File 'lib/acorn_cache/cached_response.rb', line 27 def serialize { headers: headers, status: status, body: body }.to_json end |
#time_to_live ⇒ Object Also known as: stale_time_specified?
66 67 68 |
# File 'lib/acorn_cache/cached_response.rb', line 66 def time_to_live s_maxage || max_age || (expiration_date - date) end |
#time_until_expiration ⇒ Object
92 93 94 |
# File 'lib/acorn_cache/cached_response.rb', line 92 def time_until_expiration Time.now - expiration end |
#to_a ⇒ Object
31 32 33 |
# File 'lib/acorn_cache/cached_response.rb', line 31 def to_a [status, headers, [body]] end |
#update_date! ⇒ Object
23 24 25 |
# File 'lib/acorn_cache/cached_response.rb', line 23 def update_date! headers["Date"] = Time.now.httpdate end |
#update_date_and_recache!(cache_key) ⇒ Object
43 44 45 46 47 |
# 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 |