Interface EvictableCache<K,V>
-
- Type Parameters:
K
- type of keys in this cacheV
- type of values in this cache
public interface EvictableCache<K,V>
Generic cache with eviction and max size. Cache timeouts:EvictableCache.Builder.overallTimeout(long, java.util.concurrent.TimeUnit)
defines the timeout of record since its creationEvictableCache.Builder.timeout(long, java.util.concurrent.TimeUnit)
defines the timeout of record since last use (a sliding timeout)
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
EvictableCache.Builder<K,V>
Builder to create instances ofEvictableCache
.
-
Field Summary
Fields Modifier and Type Field Description static long
CACHE_EVICT_DELAY_MINUTES
Default eviction delay in minutes (how long to wait after the cache is started).static long
CACHE_EVICT_PERIOD_MINUTES
Default eviction period in minutes (how often to evict records).static long
CACHE_MAX_SIZE
Maximal number of records in the cache.static long
CACHE_TIMEOUT_MINUTES
Default timeout of records in minutes (inactivity timeout).static long
EVICT_PARALLELISM_THRESHOLD
Parameter toConcurrentHashMap.forEachKey(long, Consumer)
used for eviction.
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <K,V>
EvictableCache.Builder<K,V>builder()
Create a new builder for a cache.default void
close()
Close this cache.default Optional<V>
computeValue(K key, Supplier<Optional<V>> valueSupplier)
Either return a cached value or compute it and cache it.static <K,V>
EvictableCache<K,V>create()
Create a new cache with default values.static <K,V>
EvictableCache<K,V>create(Config config)
Create a new cache and configure it from the provided configuration.default Optional<V>
get(K key)
Get current cached value if valid.static <K,V>
EvictableCache<K,V>noCache()
Create a new cache that is not a cache (e.g.default Optional<V>
remove(K key)
Remove a key from the cache.default int
size()
Current size of the cache.
-
-
-
Field Detail
-
CACHE_TIMEOUT_MINUTES
static final long CACHE_TIMEOUT_MINUTES
Default timeout of records in minutes (inactivity timeout).- See Also:
- Constant Field Values
-
CACHE_EVICT_PERIOD_MINUTES
static final long CACHE_EVICT_PERIOD_MINUTES
Default eviction period in minutes (how often to evict records).- See Also:
- Constant Field Values
-
CACHE_EVICT_DELAY_MINUTES
static final long CACHE_EVICT_DELAY_MINUTES
Default eviction delay in minutes (how long to wait after the cache is started).- See Also:
- Constant Field Values
-
CACHE_MAX_SIZE
static final long CACHE_MAX_SIZE
Maximal number of records in the cache. If the cache is full, no caching is done and the supplier of value is called for every uncached value.- See Also:
- Constant Field Values
-
EVICT_PARALLELISM_THRESHOLD
static final long EVICT_PARALLELISM_THRESHOLD
Parameter toConcurrentHashMap.forEachKey(long, Consumer)
used for eviction.- See Also:
- Constant Field Values
-
-
Method Detail
-
builder
static <K,V> EvictableCache.Builder<K,V> builder()
Create a new builder for a cache.- Type Parameters:
K
- type of keys in the cacheV
- type of values in the cache- Returns:
- a builder to build the cache
-
create
static <K,V> EvictableCache<K,V> create()
Create a new cache with default values.- Type Parameters:
K
- type of keys in the cacheV
- type of values in the cache- Returns:
- new cache built with default values
-
create
static <K,V> EvictableCache<K,V> create(Config config)
Create a new cache and configure it from the provided configuration. SeeEvictableCache.Builder.config(Config)
for the list of configuration keys.- Type Parameters:
K
- type of keys in the cacheV
- type of values in the cache- Parameters:
config
- config to read configuration of this cache from- Returns:
- new cache configured from config
-
noCache
static <K,V> EvictableCache<K,V> noCache()
Create a new cache that is not a cache (e.g. never caches, delegates all calls to theSupplier
incomputeValue(Object, Supplier)
.- Type Parameters:
K
- Type of keysV
- Type of values- Returns:
- a new instance that is not caching
-
remove
default Optional<V> remove(K key)
Remove a key from the cache. Return the value if it was cached and valid.- Parameters:
key
- key to remove- Returns:
- value if it was removed and valid, empty otherwise
-
get
default Optional<V> get(K key)
Get current cached value if valid.- Parameters:
key
- key to use- Returns:
- current value in the cache or empty if not present (or invalid)
-
size
default int size()
Current size of the cache. As this cache is usingConcurrentHashMap
as backing store, be aware that this value is not guaranteed to be consistent, as puts and removed may be happening in parallel.- Returns:
- current size of the cache (including valid and invalid - not yet evicted - values)
-
computeValue
default Optional<V> computeValue(K key, Supplier<Optional<V>> valueSupplier)
Either return a cached value or compute it and cache it.- Parameters:
key
- key to check/insert value forvalueSupplier
- supplier called if the value is not yet cached, or is invalid- Returns:
- current value from the cache, or computed value from the supplier
-
close
default void close()
Close this cache. Carry out shutdown tasks (e.g. shutting down eviction thread).
-
-