Package org.openbravo.cache
Class TimeInvalidatedCacheBuilder<K,V>
- java.lang.Object
-
- org.openbravo.cache.TimeInvalidatedCacheBuilder<K,V>
-
- Type Parameters:
K
- Key type used in cacheV
- Value type used in cache
public class TimeInvalidatedCacheBuilder<K,V> extends Object
Allows building a TimeInvalidatedCache and initializing it. Do not instantiate directly, instead useTimeInvalidatedCache.newBuilder()
method to create a new instance of this class. Expects to be called as follows:TimeInvalidatedCache.newBuilder() .name("CacheName") // Required name of the cache .expireAfterDuration(Duration.ofMinutes(5)) // Could be any Duration, not necessarily in // minutes. If not executed, 1 minute default // is assumed .build(key -> generateKeyValue(key)) // This is a lambda that initializes the key if it * // expired or is the first time reading it. It is * // required.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <K1 extends K,V1 extends V>
TimeInvalidatedCache<K1,V1>build(Function<? super K1,V1> loader)
Builds the TimeInvalidatedCacheTimeInvalidatedCacheBuilder<K,V>
expireAfterDuration(Duration duration)
Sets the expiration duration, after this period the key is considered expired and reloaded on the next access.TimeInvalidatedCacheBuilder<K,V>
name(String nameToSet)
Sets the name of the cache, used for logging purposes, it is always requiredTimeInvalidatedCacheBuilder<K,V>
removalListener(BiConsumer<Map.Entry<K,V>,String> listener)
Sets the removal listener which is notified every time a cache entry is invalidated.
-
-
-
Method Detail
-
build
public <K1 extends K,V1 extends V> TimeInvalidatedCache<K1,V1> build(Function<? super K1,V1> loader)
Builds the TimeInvalidatedCache- Parameters:
loader
- lambda that initializes the key if it expired or is the first time it is read. It should receive a key and return the value corresponding to it- Returns:
TimeInvalidatedCache
fully built object- Throws:
OBException
- If name or loader have not been set previous to executing the build functionIllegalArgumentException
- if duration is negative (previously executing expireDuration())IllegalStateException
- if the time to live or variable expiration was already set (previously executing expireDuration())ArithmeticException
- for durations greater than +/- approximately 292 year (previously executing expireDuration())- See Also:
TimeInvalidatedCacheBuilder
-
name
public TimeInvalidatedCacheBuilder<K,V> name(String nameToSet)
Sets the name of the cache, used for logging purposes, it is always required- Parameters:
nameToSet
- Cache name- Returns:
- this object
-
expireAfterDuration
public TimeInvalidatedCacheBuilder<K,V> expireAfterDuration(Duration duration)
Sets the expiration duration, after this period the key is considered expired and reloaded on the next access. If not invoked, defaults to 1 minute.- Parameters:
duration
- Duration of time after which is considered expired- Returns:
- this object
-
removalListener
public TimeInvalidatedCacheBuilder<K,V> removalListener(BiConsumer<Map.Entry<K,V>,String> listener)
Sets the removal listener which is notified every time a cache entry is invalidated.- Parameters:
listener
- The removal listener. It is aBiConsumer
with the invalidated entry as first argument and the removal cause as second argument.- Returns:
- this object
-
-