Module: HTTPX::Plugins::Cache::InstanceMethods
- Defined in:
- lib/httpx/plugins/cache.rb,
sig/plugins/cache.rbs
Instance Method Summary collapse
- #build_request ⇒ Object
-
#cacheable_request?(request) ⇒ Boolean
whether
requestcan use cached responses. -
#cacheable_response?(request, response) ⇒ Boolean
whether the retrieved
responsecan be cached. -
#clear_response_cache ⇒ void
wipes out all cached responses from the cache store.
-
#prepare_cache(request) ⇒ void
will either assign a still-fresh cached response to
request, or set up its HTTP cache invalidation headers in case it's not fresh anymore. -
#retrieve_cached_response(request) ⇒ cacheResponse?
calls the cache store to retrieve the cached response for
request. -
#valid_cached_response?(request, cached_response) ⇒ Boolean
whether the cached
cached_responseis still valid for the currentrequest.
Instance Method Details
#build_request ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/httpx/plugins/cache.rb', line 89 def build_request(*) request = super return request unless cacheable_request?(request) prepare_cache(request) request end |
#cacheable_request?(request) ⇒ Boolean
whether request can use cached responses.
120 121 122 123 124 |
# File 'lib/httpx/plugins/cache.rb', line 120 def cacheable_request?(request) return false unless (call = request..cacheable_request) call[request] end |
#cacheable_response?(request, response) ⇒ Boolean
whether the retrieved response can be cached.
127 128 129 130 131 |
# File 'lib/httpx/plugins/cache.rb', line 127 def cacheable_response?(request, response) return false unless (call = request..cacheable_response) call[request, response] end |
#clear_response_cache ⇒ void
This method returns an undefined value.
wipes out all cached responses from the cache store.
85 86 87 |
# File 'lib/httpx/plugins/cache.rb', line 85 def clear_response_cache @options.response_cache_store.clear end |
#prepare_cache(request) ⇒ void
This method returns an undefined value.
will either assign a still-fresh cached response to request, or set up its HTTP
cache invalidation headers in case it's not fresh anymore.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/httpx/plugins/cache.rb', line 142 def prepare_cache(request) cached_response = retrieve_cached_response(request) return unless cached_response && valid_cached_response?(request, cached_response) request.cached_response = nil # if the cached response is still usable, we use it cached_response.body.rewind cached_response = cached_response.dup cached_response.mark_as_cached! request.response = cached_response request.emit_response(cached_response) end |
#retrieve_cached_response(request) ⇒ cacheResponse?
calls the cache store to retrieve the cached response for request. Caches it
for convenience of subplugins in order to minimize overhead of retrieval (which may
involve network).
160 161 162 |
# File 'lib/httpx/plugins/cache.rb', line 160 def retrieve_cached_response(request) request.cached_response ||= request..response_cache_store.get(request) end |
#valid_cached_response?(request, cached_response) ⇒ Boolean
whether the cached cached_response is still valid for the current request
134 135 136 137 138 |
# File 'lib/httpx/plugins/cache.rb', line 134 def valid_cached_response?(request, cached_response) return false unless (call = request..valid_cached_response) call[request, cached_response] end |