Class AsyncRefreshableSupplier<V,​E extends Exception>

  • Type Parameters:
    V - input type of the function parameter
    E - supported exception type of the function parameter

    public final class AsyncRefreshableSupplier<V,​E extends Exception>
    extends Object
    This utility class supports a lock-downgrading strategy to make sure that 2 concurrent calls to compute the "cached" value will result in a single computation. It supports concurrent refreshment.
    • Constructor Detail

      • AsyncRefreshableSupplier

        public AsyncRefreshableSupplier​(AsyncFunction<Context,​V,​E> compute)
        Creates a provider ensuring "only-once" invocation of the given compute function.
        Parameters:
        compute - computation function
    • Method Detail

      • get

        public Promise<V,​E> get​(Context context)
        Computes (or re-computes) the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed.
        Parameters:
        context - Service context
        Returns:
        computed promise
      • refresh

        public Promise<V,​E> refresh​(Context context)
        Refresh (and returns) the value, in a thread-safe manner.

        It works like this:

        • The thread try to take the refresh lock, if it succeed that means that no refresh is in progress, therefore it can safely invalidate the value and returns the newly computed value.
        • Other concurrent threads will not have the lock (using the else branch of the condition) and will all be together waiting for the lock to be released.
        • When the lock is released, all concurrent threads are notified (one after the other) and returns the refreshed value.
        Parameters:
        context - Service context
        Returns:
        refreshed value
      • getWithoutRefresh

        public Promise<V,​E> getWithoutRefresh()
        Computes the value in a thread-safe manner, making sure that only 1 thread will compute the value, the other being blocked, waiting for the result to be computed. If the value needs to be refreshed null is returned.
        Returns:
        computed promise or null if the promise is not valid and needs refresh.
      • invalidate

        public void invalidate()
        Flag the current token as being invalid to force a refresh on next get().