Class InMemoryCache

java.lang.Object
com.shri.restinpeace.cache.InMemoryCache
All Implemented Interfaces:
Cache

public final class InMemoryCache extends Object implements 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

    Constructors
    Constructor
    Description
    Creates a cache with no time-based eviction at all - an entry lives until explicitly evict(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 than maxEntryAgeMillis, checked lazily on the next get(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 Type
    Method
    Description
    void
    Removes every stored entry.
    void
    Removes the entry for key, if any - e.g. after a mutating call (a POST/PUT/DELETE) that's known to invalidate a previously-cached GET.
    get(String key)
    Returns the entry stored for key, whether or not it's still fresh - a stale entry may still be usable for revalidation (see CachedResponse.isFresh()).
    void
    put(String key, CachedResponse response)
    Stores (or replaces) the entry for key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • InMemoryCache

      public InMemoryCache()
      Creates a cache with no time-based eviction at all - an entry lives until explicitly evict(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 than maxEntryAgeMillis, checked lazily on the next get(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 for ETag/Last-Modified revalidation (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 from CachedResponse.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

      public CachedResponse get(String key)
      Description copied from interface: Cache
      Returns the entry stored for key, whether or not it's still fresh - a stale entry may still be usable for revalidation (see CachedResponse.isFresh()).
      Specified by:
      get in interface Cache
      Parameters:
      key - the cache key
      Returns:
      the stored entry, or null if nothing is cached for it
    • put

      public void put(String key, CachedResponse response)
      Description copied from interface: Cache
      Stores (or replaces) the entry for key.
      Specified by:
      put in interface Cache
      Parameters:
      key - the cache key
      response - the entry to store
    • evict

      public void evict(String key)
      Description copied from interface: Cache
      Removes the entry for key, if any - e.g. after a mutating call (a POST/PUT/DELETE) that's known to invalidate a previously-cached GET.
      Specified by:
      evict in interface Cache
      Parameters:
      key - the cache key to remove
    • clear

      public void clear()
      Description copied from interface: Cache
      Removes every stored entry.
      Specified by:
      clear in interface Cache