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
222 def initialize(*)
223   super
224   @cached = false
225 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
292 def cache_control
293   return @cache_control if defined?(@cache_control)
294 
295   @cache_control = begin
296     return unless @headers.key?("cache-control")
297 
298     @headers["cache-control"].split(/ *, */)
299   end
300 end
cached?()

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

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

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

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

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

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

a copy of the request this response was originally cached from

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

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

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