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
225 def initialize(*)
226   super
227   @cached = false
228 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
295 def cache_control
296   return @cache_control if defined?(@cache_control)
297 
298   @cache_control = begin
299     return unless @headers.key?("cache-control")
300 
301     @headers["cache-control"].split(/ *, */)
302   end
303 end
cached?()

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

[show source]
    # File lib/httpx/plugins/response_cache.rb
236 def cached?
237   @cached
238 end
copy_from_cached!()

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

[show source]
    # File lib/httpx/plugins/response_cache.rb
246 def copy_from_cached!
247   cached_response = @request.cached_response
248 
249   return unless cached_response
250 
251   # 304 responses do not have content-type, which are needed for decoding.
252   @headers = @headers.class.new(cached_response.headers.merge(@headers))
253 
254   @body = cached_response.body.dup
255 
256   @body.rewind
257 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
262 def fresh?
263   if cache_control
264     return false if cache_control.include?("no-cache")
265 
266     return true if cache_control.include?("immutable")
267 
268     # check age: max-age
269     max_age = cache_control.find { |directive| directive.start_with?("s-maxage") }
270 
271     max_age ||= cache_control.find { |directive| directive.start_with?("max-age") }
272 
273     max_age = max_age[/age=(\d+)/, 1] if max_age
274 
275     max_age = max_age.to_i if max_age
276 
277     return max_age > age if max_age
278   end
279 
280   # check age: expires
281   if @headers.key?("expires")
282     begin
283       expires = Time.httpdate(@headers["expires"])
284     rescue ArgumentError
285       return false
286     end
287 
288     return (expires - Time.now).to_i.positive?
289   end
290 
291   false
292 end
mark_as_cached!()

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

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

a copy of the request this response was originally cached from

[show source]
    # File lib/httpx/plugins/response_cache.rb
231 def original_request
232   @original_request || @request
233 end
vary()

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

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