module HTTPX::Plugins::ResponseCache::ResponseMethods

  1. lib/httpx/plugins/response_cache.rb

Methods

Public Instance

  1. cache_control
  2. copy_from_cached
  3. fresh?
  4. vary

Public Instance methods

cache_control()
[show source]
    # File lib/httpx/plugins/response_cache.rb
144 def cache_control
145   return @cache_control if defined?(@cache_control)
146 
147   @cache_control = begin
148     return unless @headers.key?("cache-control")
149 
150     @headers["cache-control"].split(/ *, */)
151   end
152 end
copy_from_cached(other)
[show source]
    # File lib/httpx/plugins/response_cache.rb
104 def copy_from_cached(other)
105   # 304 responses do not have content-type, which are needed for decoding.
106   @headers = @headers.class.new(other.headers.merge(@headers))
107 
108   @body = other.body.dup
109 
110   @body.rewind
111 end
fresh?()

A response is fresh if its age has not yet exceeded its freshness lifetime.

[show source]
    # File lib/httpx/plugins/response_cache.rb
114 def fresh?
115   if cache_control
116     return false if cache_control.include?("no-cache")
117 
118     # check age: max-age
119     max_age = cache_control.find { |directive| directive.start_with?("s-maxage") }
120 
121     max_age ||= cache_control.find { |directive| directive.start_with?("max-age") }
122 
123     max_age = max_age[/age=(\d+)/, 1] if max_age
124 
125     max_age = max_age.to_i if max_age
126 
127     return max_age > age if max_age
128   end
129 
130   # check age: expires
131   if @headers.key?("expires")
132     begin
133       expires = Time.httpdate(@headers["expires"])
134     rescue ArgumentError
135       return true
136     end
137 
138     return (expires - Time.now).to_i.positive?
139   end
140 
141   true
142 end
vary()
[show source]
    # File lib/httpx/plugins/response_cache.rb
154 def vary
155   return @vary if defined?(@vary)
156 
157   @vary = begin
158     return unless @headers.key?("vary")
159 
160     @headers["vary"].split(/ *, */)
161   end
162 end