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

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

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

Returns a new instance of Response.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/async/http/cache/response.rb', line 18

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.



35
36
37
# File 'lib/async/http/cache/response.rb', line 35

def generated_at
  @generated_at
end

Instance Method Details

#ageObject



41
42
43
# File 'lib/async/http/cache/response.rb', line 41

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

#dupObject



51
52
53
54
55
56
57
58
# File 'lib/async/http/cache/response.rb', line 51

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

#etagObject



37
38
39
# File 'lib/async/http/cache/response.rb', line 37

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

#expired?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/async/http/cache/response.rb', line 45

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