Class ExpiringCache<V>

  • Type Parameters:
    V - the type of the value

    @NonNullByDefault
    public class ExpiringCache<V>
    extends Object
    This is a simple expiring and reloading cache implementation. There must be provided an action in order to retrieve/calculate the value. This action will be called only if the answer from the last calculation is not valid anymore, i.e. if it is expired.
    Author:
    Christoph Weitkamp - Initial contribution, Martin van Wingerden - Add Duration constructor
    • Constructor Detail

      • ExpiringCache

        public ExpiringCache​(Duration expiry,
                             Supplier<@Nullable V> action)
        Create a new instance.
        Parameters:
        expiry - the duration for how long the value stays valid
        action - the action to retrieve/calculate the value
        Throws:
        IllegalArgumentException - For an expire value <=0.
      • ExpiringCache

        public ExpiringCache​(long expiry,
                             Supplier<@Nullable V> action)
        Create a new instance.
        Parameters:
        expiry - the duration in milliseconds for how long the value stays valid
        action - the action to retrieve/calculate the value
    • Method Detail

      • getValue

        public @Nullable V getValue()
        Returns the value - possibly from the cache, if it is still valid.
      • putValue

        public final void putValue​(@Nullable V value)
        Puts a new value into the cache.
        Parameters:
        value - the new value
      • invalidateValue

        public final void invalidateValue()
        Invalidates the value in the cache.
      • refreshValue

        public @Nullable V refreshValue()
        Refreshes and returns the value in the cache.
        Returns:
        the new value
      • isExpired

        public boolean isExpired()
        Checks if the value is expired.
        Returns:
        true if the value is expired