Package com.shri.restinpeace.cache
Class CachedResponse
java.lang.Object
com.shri.restinpeace.cache.CachedResponse
A stored
GET response, as handed to and returned from a
Cache implementation. Immutable - a revalidated or newly-stored
entry is always a new instance, never a mutation of an existing one.-
Constructor Summary
ConstructorsConstructorDescriptionCachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis) Creates a cached entry with noVaryheader - equivalent toCachedResponse(int, Map, String, long, Map)with an empty map.CachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis, Map<String, String> varyRequestHeaders) Creates a cached entry, snapshotting the request header values its response's ownVaryheader named - so a later request whose values for those same headers differ is never served this entry.CachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis, Map<String, String> varyRequestHeaders, long staleWhileRevalidateUntilEpochMillis) Creates a cached entry with an explicit stale-while-revalidate deadline - aCache-Control: stale-while-revalidate=Nresponse may still be served, stale, up until this epoch millisecond, while a background revalidation refreshes the entry for the next call (seeisWithinStaleWhileRevalidateWindow()). -
Method Summary
Modifier and TypeMethodDescriptiongetBody()Returns the original response body.longReturns the epoch millisecond after which this entry is stale.Returns the first value of a response header, matched case-insensitively.Returns the original response's headers, keyed case-insensitively.longReturns the epoch millisecond up to which this entry may still be served stale while a background revalidation refreshes it, per aCache-Control: stale-while-revalidate=Nresponse - equal togetFreshUntilEpochMillis()(no window at all) for an entry whose response named no such directive.intReturns the original response's HTTP status code.longReturns when this entry was constructed - at initial storage, or the moment a stale entry was last successfully revalidated (a freshCachedResponseinstance is created either way; this class is immutable).Returns the request header values this entry'sVaryheader named, snapshotted at storage time.booleanisFresh()Whether this entry can still be served without revalidating against the origin server.booleanWhether this entry is stale (isFresh()isfalse) but still within itsstale-while-revalidatewindow - safe to serve immediately while a background revalidation refreshes it, rather than blocking the caller on a synchronous revalidation round trip.
-
Constructor Details
-
CachedResponse
public CachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis) Creates a cached entry with noVaryheader - equivalent toCachedResponse(int, Map, String, long, Map)with an empty map.- Parameters:
status- the original response's HTTP status codeheaders- the original response's headers, keyed case-insensitivelybody- the original response bodyfreshUntilEpochMillis- the epoch millisecond after which this entry is stale and must be revalidated (or re-fetched) before being served again - already in the past for an entry that's only cached for revalidation (e.g.Cache-Control: no-cache, or a response with anETag/Last-Modifiedbut nomax-age)
-
CachedResponse
public CachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis, Map<String, String> varyRequestHeaders) Creates a cached entry, snapshotting the request header values its response's ownVaryheader named - so a later request whose values for those same headers differ is never served this entry. Equivalent toCachedResponse(int, Map, String, long, Map, long)withfreshUntilEpochMillisas the stale-while-revalidate deadline too - i.e. no stale-while-revalidate window at all, this class's original behavior.- Parameters:
status- the original response's HTTP status codeheaders- the original response's headers, keyed case-insensitivelybody- the original response bodyfreshUntilEpochMillis- the epoch millisecond after which this entry is stale and must be revalidated (or re-fetched) before being served againvaryRequestHeaders- the request header values named by this response's ownVaryheader, at the time this entry was stored - empty if the response had noVaryheader (this entry then matches any request)
-
CachedResponse
public CachedResponse(int status, Map<String, List<String>> headers, String body, long freshUntilEpochMillis, Map<String, String> varyRequestHeaders, long staleWhileRevalidateUntilEpochMillis) Creates a cached entry with an explicit stale-while-revalidate deadline - aCache-Control: stale-while-revalidate=Nresponse may still be served, stale, up until this epoch millisecond, while a background revalidation refreshes the entry for the next call (seeisWithinStaleWhileRevalidateWindow()).- Parameters:
status- the original response's HTTP status codeheaders- the original response's headers, keyed case-insensitivelybody- the original response bodyfreshUntilEpochMillis- the epoch millisecond after which this entry is stale and must be revalidated (or re-fetched) before being served againvaryRequestHeaders- the request header values named by this response's ownVaryheader, at the time this entry was stored - empty if the response had noVaryheaderstaleWhileRevalidateUntilEpochMillis- the epoch millisecond up to which this entry may still be served stale while revalidating in the background - equal tofreshUntilEpochMillis(no window) if the response named nostale-while-revalidatedirective
-
-
Method Details
-
getStatus
public int getStatus()Returns the original response's HTTP status code.- Returns:
- the status code
-
getHeaders
Returns the original response's headers, keyed case-insensitively.- Returns:
- the headers
-
getHeader
Returns the first value of a response header, matched case-insensitively.- Parameters:
name- the header name- Returns:
- the header's first value, or
nullif it wasn't sent
-
getBody
Returns the original response body.- Returns:
- the response body
-
getFreshUntilEpochMillis
public long getFreshUntilEpochMillis()Returns the epoch millisecond after which this entry is stale.- Returns:
- the freshness deadline, in epoch milliseconds
-
isFresh
public boolean isFresh()Whether this entry can still be served without revalidating against the origin server.- Returns:
trueif this entry is still fresh
-
getStaleWhileRevalidateUntilEpochMillis
public long getStaleWhileRevalidateUntilEpochMillis()Returns the epoch millisecond up to which this entry may still be served stale while a background revalidation refreshes it, per aCache-Control: stale-while-revalidate=Nresponse - equal togetFreshUntilEpochMillis()(no window at all) for an entry whose response named no such directive.- Returns:
- the stale-while-revalidate deadline, in epoch milliseconds
-
isWithinStaleWhileRevalidateWindow
public boolean isWithinStaleWhileRevalidateWindow()Whether this entry is stale (isFresh()isfalse) but still within itsstale-while-revalidatewindow - safe to serve immediately while a background revalidation refreshes it, rather than blocking the caller on a synchronous revalidation round trip.- Returns:
trueif this entry is stale but still stale-while-revalidate-eligible
-
getVaryRequestHeaders
Returns the request header values this entry'sVaryheader named, snapshotted at storage time.- Returns:
- the vary snapshot, keyed by header name - empty if the
response had no
Varyheader
-
getStoredAtEpochMillis
public long getStoredAtEpochMillis()Returns when this entry was constructed - at initial storage, or the moment a stale entry was last successfully revalidated (a freshCachedResponseinstance is created either way; this class is immutable). A distinct notion of time fromgetFreshUntilEpochMillis(): this is "how long has RIP been holding onto this at all" (what aCacheimplementation wanting to bound its own memory use, likeInMemoryCache's optional max-entry-age, would check), not "is it still safe to serve without asking the server" - an entry stored for revalidation only (e.g.Cache-Control: no-cache) is stale from the moment it's stored, by design, but its stored time still starts counting fresh here.- Returns:
- the epoch millisecond this entry was created
-