Package org.openhab.core.cache
Class ExpiringCacheAsync<V>
java.lang.Object
org.openhab.core.cache.ExpiringCacheAsync<V>
- Type Parameters:
V
- the type of the cached value
Complementary class to
ExpiringCache
, implementing an asynchronous variant
of an expiring cache. An instance returns the cached value immediately to the callback if not expired yet, otherwise
issue a fetch in another thread and notify callback implementors asynchronously.- Author:
- David Graeff - Initial contribution, Martin van Wingerden - Add Duration constructor
-
Field Summary
Modifier and TypeFieldDescriptionprotected @Nullable CompletableFuture
<V> protected long
protected final long
protected @Nullable V
-
Constructor Summary
ConstructorDescriptionExpiringCacheAsync
(long expiry) Create a new instance.ExpiringCacheAsync
(Duration expiry) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected long
Returns an arbitrary time reference in nanoseconds.@Nullable V
Return the raw value, no matter if it is already expired or still valid.getValue
(Supplier<CompletableFuture<V>> requestNewValueFuture) Returns the value - possibly from the cache, if it is still valid.void
Invalidates the value in the cache.boolean
Checks if the value is expired.refreshValue
(Supplier<CompletableFuture<V>> requestNewValueFuture) Refreshes and returns the value asynchronously.
-
Field Details
-
expiry
protected final long expiry -
expiresAt
protected long expiresAt -
currentNewValueRequest
-
value
-
-
Constructor Details
-
ExpiringCacheAsync
Create a new instance.- Parameters:
expiry
- the duration in milliseconds for how long the value stays valid. Must be positive.- Throws:
IllegalArgumentException
- For an expirevalue <=0
.
-
ExpiringCacheAsync
public ExpiringCacheAsync(long expiry) Create a new instance.- Parameters:
expiry
- the duration in milliseconds for how long the value stays valid. Must be greater than 0.- Throws:
IllegalArgumentException
- For an expirevalue <=0
.
-
-
Method Details
-
getValue
Returns the value - possibly from the cache, if it is still valid.- Parameters:
requestNewValueFuture
- If the value is expired, this supplier is called to supply the cache with a future that on completion will update the cached value- Returns:
- the value in form of a CompletableFuture. You can for instance use it this way:
getValue().thenAccept(value->useYourValueHere(value));
. If you need the value synchronously you can usegetValue().get()
.
-
invalidateValue
public void invalidateValue()Invalidates the value in the cache. -
getCurrentNanoTime
protected long getCurrentNanoTime()Returns an arbitrary time reference in nanoseconds. This is used for the cache to determine if a value has expired. -
refreshValue
Refreshes and returns the value asynchronously. Use the return value like with getValue() to get the refreshed value.- Parameters:
requestNewValueFuture
- This supplier is called to supply the cache with a future that on completion will update the cached value. The supplier will not be used, if there is already an ongoing refresh.- Returns:
- the new value in form of a CompletableFuture.
-
isExpired
public boolean isExpired()Checks if the value is expired.- Returns:
- true if the value is expired
-
getLastKnownValue
Return the raw value, no matter if it is already expired or still valid.
-