module HTTPX::Plugins::ResponseCache::ResponseMethods

  1. lib/httpx/plugins/response_cache.rb

Attributes

Public Class methods

new(*)
[show source]
    # File lib/httpx/plugins/response_cache.rb
220 def initialize(*)
221   super
222   @cached = false
223 end

Public Instance methods

cache_control()

returns the “cache-control” directives as an Array of String(s).

[show source]
    # File lib/httpx/plugins/response_cache.rb
290 def cache_control
291   return @cache_control if defined?(@cache_control)
292 
293   @cache_control = begin
294     return unless @headers.key?("cache-control")
295 
296     @headers["cache-control"].split(/ *, */)
297   end
298 end
cached?()

whether this Response was duplicated from a previously {RequestMethods#cached_response}.

[show source]
    # File lib/httpx/plugins/response_cache.rb
231 def cached?
232   @cached
233 end
copy_from_cached!()

eager-copies the response headers and body from {RequestMethods#cached_response}.

[show source]
    # File lib/httpx/plugins/response_cache.rb
241 def copy_from_cached!
242   cached_response = @request.cached_response
243 
244   return unless cached_response
245 
246   # 304 responses do not have content-type, which are needed for decoding.
247   @headers = @headers.class.new(cached_response.headers.merge(@headers))
248 
249   @body = cached_response.body.dup
250 
251   @body.rewind
252 end
fresh?()

A response is fresh if its age has not yet exceeded its freshness lifetime. other (cache_control} directives may influence the outcome, as per the rules from the rfc

[show source]
    # File lib/httpx/plugins/response_cache.rb
257 def fresh?
258   if cache_control
259     return false if cache_control.include?("no-cache")
260 
261     return true if cache_control.include?("immutable")
262 
263     # check age: max-age
264     max_age = cache_control.find { |directive| directive.start_with?("s-maxage") }
265 
266     max_age ||= cache_control.find { |directive| directive.start_with?("max-age") }
267 
268     max_age = max_age[/age=(\d+)/, 1] if max_age
269 
270     max_age = max_age.to_i if max_age
271 
272     return max_age > age if max_age
273   end
274 
275   # check age: expires
276   if @headers.key?("expires")
277     begin
278       expires = Time.httpdate(@headers["expires"])
279     rescue ArgumentError
280       return false
281     end
282 
283     return (expires - Time.now).to_i.positive?
284   end
285 
286   false
287 end
mark_as_cached!()

sets this Response as being duplicated from a previously cached response.

[show source]
    # File lib/httpx/plugins/response_cache.rb
236 def mark_as_cached!
237   @cached = true
238 end
original_request()

a copy of the request this response was originally cached from

[show source]
    # File lib/httpx/plugins/response_cache.rb
226 def original_request
227   @original_request || @request
228 end
vary()

returns the “vary” header value as an Array of (String) headers.

[show source]
    # File lib/httpx/plugins/response_cache.rb
301 def vary
302   return @vary if defined?(@vary)
303 
304   @vary = begin
305     return unless @headers.key?("vary")
306 
307     @headers["vary"].split(/ *, */).map(&:downcase)
308   end
309 end