Package org.ametys.core.util
Class CachedValue<T>
- java.lang.Object
-
- org.ametys.core.util.CachedValue<T>
-
- Type Parameters:
T
- The type of the cached value
public class CachedValue<T> extends Object
A cached value. It can be constructed withwithInitial(java.util.function.Supplier<T>)
with aSupplier
to pass in order to compute the value to cache on the first call ofget()
.
It can also be constructed withwithExpiryDuration(java.util.function.Supplier<T>, java.time.Duration)
with theSupplier
and an expiryDuration
to automatically force the reload of the value.
The supplier is ensured to be called once (untiluncache()
is called, or after the expiry duration has elapsed since the last computing), even with multiple callers at the same time.
The returned value by the supplier must not be null
-
-
Field Summary
Fields Modifier and Type Field Description private static Object
__ALL_CACHES_ONLY_KEY
private LoadingCache<Object,T>
_loadingCache
-
Constructor Summary
Constructors Constructor Description CachedValue(Supplier<T> supplier, Duration expiryDuration, Ticker ticker)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) CacheBuilder<Object,Object>
_cacheBuilder(Duration expiryDuration, Ticker ticker)
private static <T> CacheLoader<Object,T>
_cacheLoader(Supplier<T> supplier)
T
get()
Returns the value.void
uncache()
Uncache the value.static <T> CachedValue<T>
withExpiryDuration(Supplier<T> supplier, Duration expiryDuration)
Creates a cached value.static <T> CachedValue<T>
withInitial(Supplier<T> supplier)
Creates a cached value.
-
-
-
Field Detail
-
__ALL_CACHES_ONLY_KEY
private static final Object __ALL_CACHES_ONLY_KEY
-
_loadingCache
private LoadingCache<Object,T> _loadingCache
-
-
Constructor Detail
-
CachedValue
CachedValue(Supplier<T> supplier, Duration expiryDuration, Ticker ticker)
-
-
Method Detail
-
withInitial
public static <T> CachedValue<T> withInitial(Supplier<T> supplier)
Creates a cached value. The value will be computed by invoking the given supplier when theget()
method will be called the first time.- Type Parameters:
T
- The type of the cached value- Parameters:
supplier
- The supplier which will be invoked only once in order to compute the value to cache. The value must not be null- Returns:
- The
CachedValue
-
withExpiryDuration
public static <T> CachedValue<T> withExpiryDuration(Supplier<T> supplier, Duration expiryDuration)
Creates a cached value. The value will be computed by invoking the given supplier when theget()
method will be called the very first time, and every first time after the givenDuration
is expired.- Type Parameters:
T
- The type of the cached value- Parameters:
supplier
- The supplier which will be invoked only once in order to compute the value to cache. The value must not be nullexpiryDuration
- The expiry duration- Returns:
- The
CachedValue
-
_cacheBuilder
CacheBuilder<Object,Object> _cacheBuilder(Duration expiryDuration, Ticker ticker)
-
_cacheLoader
private static <T> CacheLoader<Object,T> _cacheLoader(Supplier<T> supplier)
-
get
public T get()
Returns the value. If it is the first time it is called, then it is computed by invoking the supplier function. Otherwise, the cached value is returned.- Returns:
- the value
-
uncache
public void uncache()
Uncache the value. Mostly to force to re-compute the value.
-
-