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
A cached value. It can be constructed with
It can also be constructed with
The supplier is ensured to be called once (until
The returned value by the supplier must not be null
withInitial(java.util.function.Supplier<T>)
with a Supplier
to pass in order to compute the value to cache on the first call of get()
.
It can also be constructed with
withExpiryDuration(java.util.function.Supplier<T>, java.time.Duration)
with the Supplier
and an expiry Duration
to automatically force the reload of the value.
The supplier is ensured to be called once (until
uncache()
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
-
Method Summary
Modifier and TypeMethodDescriptionget()
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.
-
Method Details
-
withInitial
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
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
-
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
Uncache the value. Mostly to force to re-compute the value.
-