Class ExpiringCacheAsync<V>

java.lang.Object
org.openhab.core.cache.ExpiringCacheAsync<V>
Type Parameters:
V - the type of the cached value

@NonNullByDefault public class ExpiringCacheAsync<V> extends Object
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 Details

    • expiry

      protected final long expiry
    • expiresAt

      protected long expiresAt
    • currentNewValueRequest

      protected @Nullable CompletableFuture<V> currentNewValueRequest
    • value

      protected @Nullable V value
  • Constructor Details

    • ExpiringCacheAsync

      public ExpiringCacheAsync(Duration expiry)
      Create a new instance.
      Parameters:
      expiry - the duration in milliseconds for how long the value stays valid. Must be positive.
      Throws:
      IllegalArgumentException - For an expire value <=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 expire value <=0.
  • Method Details

    • getValue

      public CompletableFuture<V> getValue(Supplier<CompletableFuture<V>> requestNewValueFuture)
      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 use getValue().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

      public CompletableFuture<V> refreshValue(Supplier<CompletableFuture<V>> requestNewValueFuture)
      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

      public @Nullable V getLastKnownValue()
      Return the raw value, no matter if it is already expired or still valid.