Note: introduced in 0.18.0
.
The :response_cache
plugin makes transparent use of HTTP conditional requests and caching strategies to improve performance and bandwidth usage (at the expense of some memory usage).
Essentially, when the plugin is enabled, responses which advertise some etag
or last-modified
property will be kept in a “cached responses” store, and subsequent requests to the same URI will be performed with conditional headers in the request (if-none-match
, if-modified-since
…). If the response is cached (status code 304
), the response returned to the user will contain the body from the cached response; if the response is not cached, the last response replaces the cached response in the store.
client = HTTPX.plugin(:response_cache)
r1 = client.get("https://nghttp2.org/httpbin/cache")
r2 = client.get("https://nghttp2.org/httpbin/cache")
r1.status #=> 200
r2.status #=> 304
r1.body == r2.body #=> true
# to wipe out the response cache
client.clear_response_cache
Next: Circuit Breaker