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'
'set-cookie'
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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/async/http/cache/response.rb', line 36

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.



53
54
55
# File 'lib/async/http/cache/response.rb', line 53

def generated_at
  @generated_at
end

Instance Method Details

#ageObject



77
78
79
# File 'lib/async/http/cache/response.rb', line 77

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

#cachable?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/async/http/cache/response.rb', line 59

def cachable?
	if cache_control = @headers[CACHE_CONTROL]
		if cache_control.private? || !cache_control.public?
			return false
		end
	else
		# No cache control header...
		return false
	end
	
	if set_cookie = @headers[SET_COOKIE]
		Console.logger.warn(self) {"Cannot cache response with set-cookie header!"}
		return false
	end
	
	return true
end

#dupObject



87
88
89
90
91
92
93
94
# File 'lib/async/http/cache/response.rb', line 87

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

#etagObject



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

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

#expired?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/async/http/cache/response.rb', line 81

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