Class: HTTP::Features::Caching::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/http/features/caching/entry.rb,
sig/http.rbs

Overview

A cached response entry with freshness logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, version:, headers:, proxy_headers:, body:, request_uri:, stored_at:) ⇒ Entry

Create a new cache entry

Examples:

Entry.new(status: 200, version: "1.1", headers: headers,
          proxy_headers: proxy_headers, body: "hello",
          request_uri: uri, stored_at: Time.now)

Parameters:

  • status (Integer)
  • version (String)
  • headers (HTTP::Headers)
  • proxy_headers (HTTP::Headers)
  • body (String)
  • request_uri (HTTP::URI)
  • stored_at (Time)
  • status: (Integer)
  • version: (String)
  • headers: (Headers)
  • proxy_headers: (Headers)
  • body: (String)
  • request_uri: (URI)
  • stored_at: (Time)


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

#bodyString (readonly)

The response body as a string

Examples:

entry.body # => "<html>...</html>"

Returns:

  • (String)

    the response body



53
54
55
# File 'lib/http/features/caching/entry.rb', line 53

def body
  @body
end

#headersHTTP::Headers (readonly)

The response headers

Examples:

entry.headers

Returns:



35
36
37
# File 'lib/http/features/caching/entry.rb', line 35

def headers
  @headers
end

#proxy_headersHTTP::Headers (readonly)

The proxy headers from the original response

Examples:

entry.proxy_headers

Returns:



44
45
46
# File 'lib/http/features/caching/entry.rb', line 44

def proxy_headers
  @proxy_headers
end

#request_uriHTTP::URI (readonly)

The URI of the original request

Examples:

entry.request_uri

Returns:



62
63
64
# File 'lib/http/features/caching/entry.rb', line 62

def request_uri
  @request_uri
end

#statusInteger (readonly)

The HTTP status code

Examples:

entry.status # => 200

Returns:

  • (Integer)

    the HTTP status code



17
18
19
# File 'lib/http/features/caching/entry.rb', line 17

def status
  @status
end

#stored_atTime (readonly)

When the response was stored

Examples:

entry.stored_at

Returns:

  • (Time)

    when the response was stored



71
72
73
# File 'lib/http/features/caching/entry.rb', line 71

def stored_at
  @stored_at
end

#versionString (readonly)

The HTTP version

Examples:

entry.version # => "1.1"

Returns:

  • (String)

    the HTTP version



26
27
28
# File 'lib/http/features/caching/entry.rb', line 26

def version
  @version
end

Instance Method Details

#ageFloat

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

Returns:

  • (Float)


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_atTime?

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

Returns:

  • (Time, nil)


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

Examples:

entry.fresh? # => true

Returns:

  • (Boolean)


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_ageInteger?

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

Returns:

  • (Integer, nil)


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

Returns:

  • (Boolean)


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)

Examples:

entry.revalidate!

Returns:

  • (Time)


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

Examples:

entry.update_headers!(response.headers)

Parameters:



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