Class: HTTP::Features::Caching::Entry
- Inherits:
-
Object
- Object
- HTTP::Features::Caching::Entry
- Defined in:
- lib/http/features/caching/entry.rb,
sig/http.rbs
Overview
A cached response entry with freshness logic
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The response body as a string.
-
#headers ⇒ HTTP::Headers
readonly
The response headers.
-
#proxy_headers ⇒ HTTP::Headers
readonly
The proxy headers from the original response.
-
#request_uri ⇒ HTTP::URI
readonly
The URI of the original request.
-
#status ⇒ Integer
readonly
The HTTP status code.
-
#stored_at ⇒ Time
readonly
When the response was stored.
-
#version ⇒ String
readonly
The HTTP version.
Instance Method Summary collapse
-
#age ⇒ Float
private
Age of the entry in seconds.
-
#expires_at ⇒ Time?
private
Expiration time from Expires header.
-
#fresh? ⇒ Boolean
Whether the cached response is still fresh.
-
#initialize(status:, version:, headers:, proxy_headers:, body:, request_uri:, stored_at:) ⇒ Entry
constructor
Create a new cache entry.
-
#max_age ⇒ Integer?
private
max-age value from Cache-Control, if present.
-
#no_cache? ⇒ Boolean
private
Whether the Cache-Control includes no-cache.
-
#revalidate! ⇒ Time
Reset the stored_at time to now (after successful revalidation).
-
#update_headers!(response_headers) ⇒ void
Merge response headers from a 304 revalidation into the stored entry.
Constructor Details
#initialize(status:, version:, headers:, proxy_headers:, body:, request_uri:, stored_at:) ⇒ Entry
Create a new cache entry
89 90 91 92 93 94 95 96 97 |
# File 'lib/http/features/caching/entry.rb', line 89 def initialize(status:, version:, headers:, proxy_headers:, body:, request_uri:, stored_at:) @status = status @version = version @headers = headers @proxy_headers = proxy_headers @body = body @request_uri = request_uri @stored_at = stored_at end |
Instance Attribute Details
#body ⇒ String (readonly)
The response body as a string
53 54 55 |
# File 'lib/http/features/caching/entry.rb', line 53 def body @body end |
#headers ⇒ HTTP::Headers (readonly)
The response headers
35 36 37 |
# File 'lib/http/features/caching/entry.rb', line 35 def headers @headers end |
#proxy_headers ⇒ HTTP::Headers (readonly)
The proxy headers from the original response
44 45 46 |
# File 'lib/http/features/caching/entry.rb', line 44 def proxy_headers @proxy_headers end |
#request_uri ⇒ HTTP::URI (readonly)
The URI of the original request
62 63 64 |
# File 'lib/http/features/caching/entry.rb', line 62 def request_uri @request_uri end |
#status ⇒ Integer (readonly)
The HTTP status code
17 18 19 |
# File 'lib/http/features/caching/entry.rb', line 17 def status @status end |
#stored_at ⇒ Time (readonly)
When the response was stored
71 72 73 |
# File 'lib/http/features/caching/entry.rb', line 71 def stored_at @stored_at end |
#version ⇒ String (readonly)
The HTTP version
26 27 28 |
# File 'lib/http/features/caching/entry.rb', line 26 def version @version end |
Instance Method Details
#age ⇒ Float
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Age of the entry in seconds
146 147 148 |
# File 'lib/http/features/caching/entry.rb', line 146 def age Float(Integer(headers[Headers::AGE], exception: false) || 0) + (Time.now - stored_at) end |
#expires_at ⇒ Time?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Expiration time from Expires header
163 164 165 166 167 |
# File 'lib/http/features/caching/entry.rb', line 163 def expires_at Time.httpdate(String(headers[Headers::EXPIRES])) rescue ArgumentError nil end |
#fresh? ⇒ Boolean
Whether the cached response is still fresh
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/http/features/caching/entry.rb', line 106 def fresh? return false if no_cache? ttl = max_age return age < ttl if ttl expires = expires_at return Time.now < expires if expires false end |
#max_age ⇒ Integer?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
max-age value from Cache-Control, if present
153 154 155 156 157 158 |
# File 'lib/http/features/caching/entry.rb', line 153 def max_age match = String(headers[Headers::CACHE_CONTROL]).match(/max-age=(\d+)/) return unless match Integer(match[1]) end |
#no_cache? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether the Cache-Control includes no-cache
172 173 174 |
# File 'lib/http/features/caching/entry.rb', line 172 def no_cache? String(headers[Headers::CACHE_CONTROL]).downcase.include?("no-cache") end |
#revalidate! ⇒ Time
Reset the stored_at time to now (after successful revalidation)
125 126 127 |
# File 'lib/http/features/caching/entry.rb', line 125 def revalidate! @stored_at = Time.now end |
#update_headers!(response_headers) ⇒ void
This method returns an undefined value.
Merge response headers from a 304 revalidation into the stored entry
137 138 139 |
# File 'lib/http/features/caching/entry.rb', line 137 def update_headers!(response_headers) response_headers.each { |name, value| @headers[name] = value } # steep:ignore end |