Class CacheMap<K,​V>

  • Type Parameters:
    K - The key type
    V - The value type
    All Implemented Interfaces:
    Serializable, Map<K,​V>

    public class CacheMap<K,​V>
    extends Object
    implements Map<K,​V>, Serializable
    Implements a Cache that exposes itself as a regular Map.

    Cached entries will be removed from the map only when an operation would touch that entry, including all bulk operations (e.g. isEmpty()).

    If you serialize this class, it will be replaced with a HashMap<K,V> snapshot in the serialization stream via writeReplace()

    See Also:
    Serialized Form
    • Constructor Detail

      • CacheMap

        public CacheMap()
        Constructs a new cache map with the default expiration time of 10 minutes
      • CacheMap

        public CacheMap​(int amount,
                        TimeUnit type)
        Constructs a new empty cache map with the given expiration time in the given units.

        The time will be converted internally to seconds.

        Parameters:
        amount - The amount of the given time unit before expiration
        type - The time unit
      • CacheMap

        public CacheMap​(int amount,
                        TimeUnit type,
                        Map<? extends K,​? extends V> input)
        Constructs a new empty cache map with the given expiratino time in the given units.

        Additionally, copies all values from the given Map into this CacheMap, setting their expiration as though they had just been inserted.

        Parameters:
        amount - The amount of the given time unit before expiration
        type - The time unit
        input - An arbitrary Map to copy into this CacheMap
      • CacheMap

        public CacheMap​(int amount,
                        TimeUnit type,
                        CacheGenerator<? extends V> generator)
        Constructs a new empty cache map with the given expiration time in the given units.

        The time will be converted internally to seconds if the result is less than one second, then it will default to one second.

        Parameters:
        amount - The amount of the given time unit before expiration
        type - The time unit
        generator - The value generator to use if get() does not match a key (pass null for none)