Class: Async::HTTP::Cache::Response

Inherits:
Protocol::HTTP::Response
  • Object
show all
Defined in:
lib/async/http/cache/response.rb

Overview

Represents a cached HTTP response with cache-specific metadata and functionality.

Constant Summary collapse

CACHE_CONTROL =
"cache-control"
ETAG =
"etag"
X_CACHE =
"x-cache"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, body) ⇒ Response

Initialize a new cached response.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/http/cache/response.rb', line 22

def initialize(response, body)
	@generated_at = Async::Clock.now
	
	super(
		response.version,
		response.status,
		response.headers.flatten,
		body,
		response.protocol
	)
	
	@max_age = @headers[CACHE_CONTROL]&.max_age
	@etag = nil
	
	@headers.set(X_CACHE, "hit")
end

Instance Attribute Details

#generated_atObject (readonly)

Returns the value of attribute generated_at.



39
40
41
# File 'lib/async/http/cache/response.rb', line 39

def generated_at
  @generated_at
end

Instance Method Details

#ageObject

Calculate the age of this cached response in seconds.



49
50
51
# File 'lib/async/http/cache/response.rb', line 49

def age
	Async::Clock.now - @generated_at
end

#dupObject

Create a duplicate of this cached response with independent body and headers.



63
64
65
66
67
68
69
70
# File 'lib/async/http/cache/response.rb', line 63

def dup
	dup = super
	
	dup.body = @body.dup
	dup.headers = @headers.dup
	
	return dup
end

#etagObject

Get the ETag header value for this cached response.



43
44
45
# File 'lib/async/http/cache/response.rb', line 43

def etag
	@etag ||= @headers[ETAG]
end

#expired?Boolean

Check if this cached response has expired based on its max-age.

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/async/http/cache/response.rb', line 55

def expired?
	if @max_age
		self.age > @max_age
	end
end