java.lang.Object
io.helidon.common.configurable.LruCache<K,V>
- Type Parameters:
K
- type of the keys of the mapV
- type of the values of the map
- All Implemented Interfaces:
RuntimeType.Api<LruCacheConfig<K,
V>>
Least recently used cache.
This cache has a capacity. When the capacity is reached, the oldest record is removed from the cache when a new one
is added.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default capacity of the cache: 10000. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V> LruCacheConfig.Builder <K, V> builder()
Create a new builder.int
capacity()
Capacity of this cache.void
clear()
Clear all records in the cache.computeValue
(K key, Supplier<Optional<V>> valueSupplier) Either return a cached value or compute it and cache it.static <K,
V> LruCache <K, V> create()
Create an instance with default configuration.static <K,
V> LruCache <K, V> create
(LruCacheConfig<K, V> config) Create an instance with custom configuration.static <K,
V> LruCache <K, V> create
(Consumer<LruCacheConfig.Builder<K, V>> consumer) Create an instance with custom configuration.Get a value from the cache.The prototype as it was received when creating this runtime object instance.Put a value to the cache.Remove a value from the cache.int
size()
Current size of the map.
-
Field Details
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITYDefault capacity of the cache: 10000.- See Also:
-
-
Method Details
-
builder
Create a new builder.- Type Parameters:
K
- key typeV
- value type- Returns:
- a new fluent API builder instance
-
create
Create an instance with default configuration.- Type Parameters:
K
- key typeV
- value type- Returns:
- a new cache instance
- See Also:
-
create
Create an instance with custom configuration.- Type Parameters:
K
- key typeV
- value type- Parameters:
config
- configuration of LRU cache- Returns:
- a new cache instance
-
create
Create an instance with custom configuration.- Type Parameters:
K
- key typeV
- value type- Parameters:
consumer
- of custom configuration builder- Returns:
- a new cache instance
-
prototype
Description copied from interface:RuntimeType.Api
The prototype as it was received when creating this runtime object instance.- Specified by:
prototype
in interfaceRuntimeType.Api<K>
- Returns:
- prototype object used to create this instance
-
get
Get a value from the cache.- Parameters:
key
- key to retrieve- Returns:
- value if present or empty
-
remove
Remove a value from the cache.- Parameters:
key
- key of the record to remove- Returns:
- the value that was mapped to the key, or empty if none was
-
put
Put a value to the cache.- Parameters:
key
- key to addvalue
- value to add- Returns:
- value that was already mapped or empty if the value was not mapped
-
computeValue
Either return a cached value or compute it and cache it. In case this method is called in parallel for the same key, the value actually present in the map may be from any of the calls. This method always returns either the existing value from the map, or the value provided by the supplier. It never returns a result from another thread's supplier.- 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
-
size
public int size()Current size of the map.- Returns:
- number of records currently cached
-
capacity
public int capacity()Capacity of this cache.- Returns:
- configured capacity of this cache
-
clear
public void clear()Clear all records in the cache.
-