Class ExpiringCacheMap<K,​V>

  • Type Parameters:
    K - the type of the key
    V - the type of the value

    @NonNullByDefault
    public class ExpiringCacheMap<K,​V>
    extends Object
    This is a simple expiring and reloading multiple key-value-pair cache implementation. The value expires after the specified duration has passed since the item was created, or the most recent replacement of the value.
    Author:
    Christoph Weitkamp - Initial contribution, Martin van Wingerden - Added constructor accepting Duration and putIfAbsentAndGet
    • Constructor Detail

      • ExpiringCacheMap

        public ExpiringCacheMap​(Duration expiry)
        Creates a new instance.
        Parameters:
        expiry - the duration for how long the value stays valid
      • ExpiringCacheMap

        public ExpiringCacheMap​(long expiry)
        Creates a new instance.
        Parameters:
        expiry - the duration in milliseconds for how long the value stays valid
    • Method Detail

      • put

        public void put​(K key,
                        Supplier<@Nullable V> action)
        Creates an ExpiringCache and adds it to the cache.
        Parameters:
        key - the key with which the specified value is to be associated
        action - the action for the item to be associated with the specified key to retrieve/calculate the value
      • put

        public void put​(K key,
                        ExpiringCache<@Nullable V> item)
        Adds an ExpiringCache to the cache.
        Parameters:
        key - the key with which the specified value is to be associated
        item - the item to be associated with the specified key
      • putIfAbsent

        public void putIfAbsent​(K key,
                                ExpiringCache<V> item)
        If the specified key is not already associated with a value, associate it with the given ExpiringCache.
        Parameters:
        key - the key with which the specified value is to be associated
        item - the item to be associated with the specified key
      • putIfAbsentAndGet

        public @Nullable V putIfAbsentAndGet​(K key,
                                             Supplier<V> action)
        If the specified key is not already associated, associate it with the given action. Note that this method has the overhead of actually calling/performing the action
        Parameters:
        key - the key with which the specified value is to be associated
        action - the action for the item to be associated with the specified key to retrieve/calculate the value
        Returns:
        the (cached) value for the specified key
      • putIfAbsentAndGet

        public @Nullable V putIfAbsentAndGet​(K key,
                                             ExpiringCache<V> item)
        If the specified key is not already associated with a value, associate it with the given ExpiringCache. Note that this method has the overhead of actually calling/performing the action
        Parameters:
        key - the key with which the specified value is to be associated
        item - the item to be associated with the specified key
        Returns:
        the (cached) value for the specified key
      • putValue

        public void putValue​(K key,
                             @Nullable V value)
        Puts a new value into the cache if the specified key is present.
        Parameters:
        key - the key whose value in the cache is to be updated
        value - the new value
      • containsKey

        public boolean containsKey​(K key)
        Checks if the key is present in the cache.
        Parameters:
        key - the key whose presence in the cache is to be tested
        Returns:
        true if the cache contains a value for the specified key
      • remove

        public void remove​(K key)
        Removes the item associated with the given key from the cache.
        Parameters:
        key - the key whose associated value is to be removed
      • clear

        public void clear()
        Discards all items from the cache.
      • keys

        public Set<K> keys()
        Returns a set of all keys.
        Returns:
        the set of all keys
      • get

        public @Nullable V get​(K key)
        Returns the value associated with the given key - possibly from the cache, if it is still valid.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value associated with the given key, or null if there is no cached value for the given key
      • values

        public Collection<@Nullable V> values()
        Returns a collection of all values - possibly from the cache, if they are still valid.
        Returns:
        the collection of all values
      • invalidate

        public void invalidate​(K key)
        Invalidates the value associated with the given key in the cache.
        Parameters:
        key - the key whose associated value is to be invalidated
      • invalidateAll

        public void invalidateAll()
        Invalidates all values in the cache.
      • refresh

        public @Nullable V refresh​(K key)
        Refreshes and returns the value associated with the given key in the cache.
        Parameters:
        key - the key whose associated value is to be refreshed
        Returns:
        the value associated with the given key, or null if there is no cached value for the given key
      • refreshAll

        public Collection<@Nullable V> refreshAll()
        Refreshes and returns a collection of all new values in the cache.
        Returns:
        the collection of all values