Package com.shri.restinpeace.cache
Class InMemoryCache
java.lang.Object
com.shri.restinpeace.cache.InMemoryCache
- All Implemented Interfaces:
Cache
The default
Cache implementation: an unbounded, process-local
ConcurrentHashMap - fine for most single-process uses, but not
shared across JVMs. By default an entry is never removed except via
evict(java.lang.String)/clear() or the process exiting, same as always;
InMemoryCache(long) opts into an additional, automatic
time-based eviction on top of that, for a process that runs long enough
for entries covering many distinct, never-repeated URLs (many different
/items/{id}s that come and go) to otherwise accumulate forever.
Provide a custom Cache instead for bounded/LRU eviction by entry
count or a shared external store.-
Constructor Summary
ConstructorsConstructorDescriptionCreates a cache with no time-based eviction at all - an entry lives until explicitlyevict(java.lang.String)ed/clear()ed, no matter how long ago it was stored.InMemoryCache(long maxEntryAgeMillis) Creates a cache that also automatically forgets an entry once it's been held for longer thanmaxEntryAgeMillis, checked lazily on the nextget(java.lang.String)for that same key (there's no background thread sweeping every entry proactively, so a key that's stored once and never looked up again still isn't reclaimed until this cache itself is discarded - the same "forever, unless something touches it again" limitation the unbounded default already has, just with a smaller blast radius). -
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()).voidput(String key, CachedResponse response) Stores (or replaces) the entry forkey.
-
Constructor Details
-
InMemoryCache
public InMemoryCache()Creates a cache with no time-based eviction at all - an entry lives until explicitlyevict(java.lang.String)ed/clear()ed, no matter how long ago it was stored. Identical to this class's original behavior. -
InMemoryCache
public InMemoryCache(long maxEntryAgeMillis) Creates a cache that also automatically forgets an entry once it's been held for longer thanmaxEntryAgeMillis, checked lazily on the nextget(java.lang.String)for that same key (there's no background thread sweeping every entry proactively, so a key that's stored once and never looked up again still isn't reclaimed until this cache itself is discarded - the same "forever, unless something touches it again" limitation the unbounded default already has, just with a smaller blast radius).This is a separate clock from server-driven freshness (
CachedResponse.getFreshUntilEpochMillis()/CachedResponse.isFresh()): an entry stored only forETag/Last-Modifiedrevalidation (e.g.Cache-Control: no-cache) is stale from the moment it's stored, by design, and must keep being usable for that revalidation round trip - this age limit is purely "how long RIP has been holding onto this at all," measured fromCachedResponse.getStoredAtEpochMillis()(reset on every successful revalidation, not just the original store), independent of whether the entry is currently fresh or stale-but-revalidatable.- Parameters:
maxEntryAgeMillis- how long an entry may be held before it's treated as gone; must be positive
-
-
Method Details
-
get
Description copied from interface:CacheReturns the entry stored forkey, whether or not it's still fresh - a stale entry may still be usable for revalidation (seeCachedResponse.isFresh()). -
put
Description copied from interface:CacheStores (or replaces) the entry forkey. -
evict
Description copied from interface:CacheRemoves the entry forkey, if any - e.g. after a mutating call (aPOST/PUT/DELETE) that's known to invalidate a previously-cachedGET. -
clear
public void clear()Description copied from interface:CacheRemoves every stored entry.
-