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

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

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:



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

#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

#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