Package dev.relism.flash.ext.cache
Interface Cache<K,V>
- Type Parameters:
K- key type — must have a stablehashCode/equalsV- value type
public interface Cache<K,V>
A named cache. Obtained from a
CacheManager, safe to hold in a handler field and share
across threads.
User user = users.get(id, repo::findById);
get(K, java.util.function.Function<? super K, ? extends V>) is the only method most code needs. The rest exist for the cases it cannot
express: reading without populating, writing a value computed elsewhere, and invalidating.
-
Method Summary
Modifier and TypeMethodDescriptionlongApproximate entry count.Returns the cached value, computing and storing it withloaderif absent.getIfPresent(K key) The cached value, ornullif absent.voidinvalidate(K key) Dropskey.voidDrops every entry.voidStoresvalue, replacing any existing entry.stats()Hit/miss counters since this cache was built, orCacheStats.DISABLEDwhen the backend was not asked to record them.
-
Method Details
-
get
Returns the cached value, computing and storing it withloaderif absent.The loader runs at most once per key across concurrent callers; the others wait for it rather than each computing their own. A loader returning
nullstores nothing andnullis returned. -
getIfPresent
The cached value, ornullif absent. Never invokes a loader. -
put
Storesvalue, replacing any existing entry. -
invalidate
Dropskey. Does nothing if it was absent. -
invalidateAll
void invalidateAll()Drops every entry. -
estimatedSize
long estimatedSize()Approximate entry count. Approximate because eviction is asynchronous in most backends. -
stats
CacheStats stats()Hit/miss counters since this cache was built, orCacheStats.DISABLEDwhen the backend was not asked to record them.
-