The :cache plugin allows for caching and reusing the same HTTP response.
It does that by exposing some option callbacks which will define if and where responses are cached or requests can use a cached response.
It its the foundation of the Response Cache plugin.
:cache_keyCallable which receives a request and returns the corresponding cache key as a string (to be used by the cache store when storing cached responses)
cache_session = HTTPX.plugin(
:cache,
cache_key: ->(req) { "#{req.verb}+#{req.uri.path}" },
# ...
)
:cacheable_requestCallable which receives a request and returns whether this request can use a previously cached response, or for which a freshly retrieved response can be cached.
cache_session = HTTPX.plugin(
:cache,
cacheable_request: ->(req) { req.uri.path.include?("/cacheable") },
# ...
)
:cacheable_responseCallable which receives a request and a (freshly retrieved) response and returns whether the response can be cached.
cache_session = HTTPX.plugin(
:cache,
cacheable_response: ->(_, res) { res.status == 200 },
# ...
)
:valid_cached_responseCallable which receives a request and a (previously cached) response and returns whether the response can still be used / returned to the caller.
cache_session = HTTPX.plugin(
:cache,
valid_cached_response: ->(_, res) { check_if_not_expired(res) },
# ...
)
:response_cache_storeobject where cached responses are fetch from or stored in; defaults to :store (in-memory cache), can be set to :file_store (file system cache store) as well, or any object which abides by the Cache Store Interface.
The default :store cache store stores cached responses in memory. It is thread-safe, and can only share cached responses in the scope of the same process.
The default :file_store cache store stores cached responses in the file system (in the tempfile directory from your OS by default). It can share cached responses across several process of the same machine.
HTTPX.plugin(:response_cache, response_cache_store: :file_store)
You can pass any custom object of yours to this option which implements the Cache Store Interface:
get(request) -> returns an instance of HTTPX::Response or nil.set(request, response) -> should cache the response.clear -> deletes all cached responses from the store.Next: Response Cache