Module: HTTPX::Plugins::ResponseCache::InstanceMethods

Defined in:
lib/httpx/plugins/response_cache.rb

Instance Method Summary collapse

Instance Method Details

#build_requestObject



69
70
71
72
73
74
75
76
# File 'lib/httpx/plugins/response_cache.rb', line 69

def build_request(*)
  request = super
  return request unless ResponseCache.cacheable_request?(request) && @options.response_cache_store.cached?(request)

  @options.response_cache_store.prepare(request)

  request
end

#clear_response_cacheObject



65
66
67
# File 'lib/httpx/plugins/response_cache.rb', line 65

def clear_response_cache
  @options.response_cache_store.clear
end

#fetch_response(request) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/httpx/plugins/response_cache.rb', line 78

def fetch_response(request, *)
  response = super

  return unless response

  if ResponseCache.cached_response?(response)
    log { "returning cached response for #{request.uri}" }
    cached_response = @options.response_cache_store.lookup(request)

    response.copy_from_cached(cached_response)

  else
    @options.response_cache_store.cache(request, response)
  end

  response
end