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   @revalidated_at = nil
229 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
298 def cache_control
299   return @cache_control if defined?(@cache_control)
300 
301   @cache_control = begin
302     @headers["cache-control"].split(/ *, */) if @headers.key?("cache-control")
303   end
304 end
cached?()

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

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

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

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

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

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

a copy of the request this response was originally cached from

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

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

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