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 support. Default implementation is backed byConcurrentHashMap
and provides configuration to set this map up, as can be done throughbuilder()
, andcreate(io.helidon.config.Config)
. 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
using the default implementation backed by aConcurrentHashMap
.
-
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 that uses the default implementation.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 using the default implementation.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). Default values are valid for default implementation, custom implementations may not support such features.- 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). Default values are valid for default implementation, custom implementations may not support such features.- 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). Default values are valid for default implementation, custom implementations may not support such features.- 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. Default values are valid for default implementation, custom implementations may not support such features.- See Also:
- Constant Field Values
-
EVICT_PARALLELISM_THRESHOLD
static final long EVICT_PARALLELISM_THRESHOLD
Parameter toConcurrentHashMap.forEachKey(long, Consumer)
used for eviction. Default values are valid for default implementation, custom implementations may not support such features.- See Also:
- Constant Field Values
-
-
Method Detail
-
builder
static <K,V> EvictableCache.Builder<K,V> builder()
Create a new builder for a cache that uses the default implementation.- 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 using the default implementation.- 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. This will use the default implementation.- 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. This value may not represent exact current size, as the implementation is expected to be thread safe and accessed from multiple threads, that may change the size 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).
-
-