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 by
ConcurrentHashMap
and provides
configuration to set this map up, as can be done through builder()
,
and create(io.helidon.common.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
Modifier and TypeInterfaceDescriptionstatic class
Builder to create instances ofEvictableCache
using the default implementation backed by aConcurrentHashMap
. -
Field Summary
Modifier and TypeFieldDescriptionstatic final long
Default eviction delay in minutes (how long to wait after the cache is started).static final long
Default eviction period in minutes (how often to evict records).static final long
Maximal number of records in the cache.static final long
Default timeout of records in minutes (inactivity timeout).static final long
Parameter toConcurrentHashMap.forEachKey(long, Consumer)
used for eviction. -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.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 a new cache and configure it from the provided configuration.Get current cached value if valid.static <K,
V> EvictableCache <K, V> noCache()
Create a new cache that is not a cache (e.g.Remove a key from the cache.default int
size()
Current size of the cache.
-
Field Details
-
CACHE_TIMEOUT_MINUTES
static final long CACHE_TIMEOUT_MINUTESDefault timeout of records in minutes (inactivity timeout). Default values are valid for default implementation, custom implementations may not support such features.- See Also:
-
CACHE_EVICT_PERIOD_MINUTES
static final long CACHE_EVICT_PERIOD_MINUTESDefault 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:
-
CACHE_EVICT_DELAY_MINUTES
static final long CACHE_EVICT_DELAY_MINUTESDefault 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:
-
CACHE_MAX_SIZE
static final long CACHE_MAX_SIZEMaximal 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:
-
EVICT_PARALLELISM_THRESHOLD
static final long EVICT_PARALLELISM_THRESHOLDParameter toConcurrentHashMap.forEachKey(long, Consumer)
used for eviction. Default values are valid for default implementation, custom implementations may not support such features.- See Also:
-
-
Method Details
-
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
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
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
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
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
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
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).
-