Interface Cache
- All Known Implementing Classes:
InMemoryCache
GET response caching, attached via
RipClientConfig.Builder.cache(Cache) (one
client) or RIP.setCache(Cache) (the shared
default for every client without its own). RIP honors whatever the server
actually says via Cache-Control/ETag/Last-Modified
- a Cache implementation is purely storage, not a policy decision;
see InMemoryCache for the default, in-memory implementation
(which also supports an optional, opt-in max entry age - "eviction on the
basis of time" as an automatic background concern, distinct from the
manual eviction described below).
Keyed by key(HTTPMethod, String) - "<HTTP method> <request
URL>" (e.g. "GET https://api.example.com/items?page=2") - a
single slot per key, the newest variant replacing the previous one, rather
than storing every Vary-distinguished variant at once. A response
naming a Vary header is still never served to a request whose
current value for that header differs from the one snapshotted when it
was stored (see CachedResponse.getVaryRequestHeaders()) - this
affects only how many variants stay cached at once, not correctness.
By default the key's URL includes the query string, so
/items?page=1 and /items?page=2 are cached separately -
this can be turned off per client (see
RipClientConfig.Builder.cacheKeyIncludesQueryString(boolean))
or as a shared default (see
RIP.setCacheKeyIncludesQueryString(boolean))
for an endpoint whose query params don't affect the response (e.g. an
analytics/tracking param), trading that precision for a higher hit rate -
every query-string variant of the same path then shares one entry.
Manual eviction: a consumer holding a reference to the same
Cache instance RIP is using (since they're the one who
constructed and attached it) can force one specific call's cached
response gone at any time - myCache.evict(Cache.key(HTTPMethod.GET,
"https://api.example.com/items/42")) - e.g. right after some other
action they know invalidates it server-side, or clear() for
everything at once. Deliberately manual only: RIP itself never inspects a
POST/PUT/DELETE call to guess which cached
GETs it might have invalidated - a URL-matching heuristic at
best, wrong in either direction (a write to a different resource that
happens to share a path prefix; a write that invalidates a completely
different endpoint's cached list view) - so that decision is left to
whoever actually knows their own API's real read/write relationships.
Implementations must be safe for concurrent use - a shared Cache
(or the RIP.setCache(Cache) default) can be
read and written from multiple calls at once.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes every stored entry.voidRemoves the entry forkey, if any - e.g. after a mutating call (aPOST/PUT/DELETE) that's known to invalidate a previously-cachedGET.Returns the entry stored forkey, whether or not it's still fresh - a stale entry may still be usable for revalidation (seeCachedResponse.isFresh()).static Stringkey(HTTPMethod httpMethod, String url) Computes the cache key RIP itself uses internally for a call tourl- the same formulaget/put/evictare keyed by for that exact call, so this is the value to pass toevict(java.lang.String)to manually invalidate one specific cached response.voidput(String key, CachedResponse response) Stores (or replaces) the entry forkey.
-
Method Details
-
key
Computes the cache key RIP itself uses internally for a call tourl- the same formulaget/put/evictare keyed by for that exact call, so this is the value to pass toevict(java.lang.String)to manually invalidate one specific cached response.By default,
urlmust be the exact absolute URL as sent on the wire, query string included in the same order/encoding RIP itself would produce (e.g. from@QueryParam/@QueryMapapplied in method-declaration order) - a mismatched query string computes a different key and silently won't evict anything. For a method with no query parameters at all, this is simply the interface method's resolved URL. If the relevant client hascacheKeyIncludesQueryStringset tofalse(see the class javadoc above), pass the URL without its query string instead, since that's what RIP itself keyed the entry by.- Parameters:
httpMethod- the call's HTTP method - alwaysGETin practice, since onlyGETresponses are ever cached, but accepted generally so this formula has exactly one implementationurl- the exact request URL RIP keyed this entry by - the full URL with its query string by default, or without one ifcacheKeyIncludesQueryStringis turned off for the relevant client- Returns:
- the cache key for that call
-
get
Returns the entry stored forkey, whether or not it's still fresh - a stale entry may still be usable for revalidation (seeCachedResponse.isFresh()).- Parameters:
key- the cache key- Returns:
- the stored entry, or
nullif nothing is cached for it
-
put
Stores (or replaces) the entry forkey.- Parameters:
key- the cache keyresponse- the entry to store
-
evict
Removes the entry forkey, if any - e.g. after a mutating call (aPOST/PUT/DELETE) that's known to invalidate a previously-cachedGET.- Parameters:
key- the cache key to remove
-
clear
void clear()Removes every stored entry.
-