Class CacheMap<K,​V>

    • 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)
    • Method Detail

      • apply

        public Optional<Vapply​(K k)
        Returns an Optional wrapping a non-expired value, for use in streams, for example.
        Specified by:
        apply in interface Function<K,​V>
        Parameters:
        k - The key
        Returns:
        An Optional of the value, if it is present and non-expired, otherwise an empty Optional
      • beforeExpiration

        protected void beforeExpiration​(K key,
                                        V value)
        The default initial before expiration hook.

        By default, this does nothing, but subclasses can override it to do whatever they’d like.

        Parameters:
        key - The key being expired
        value - The value being expired
      • cache

        protected CacheEntry<Vcache​(V value)
        Caches the given value with the default expiration seconds
        Parameters:
        value - The value to cache
        Returns:
        A cache entry representing this value
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if the map contains the given key and it is not expired.

        If the map does not contain the key, and a value generator is defined, it will be invoked, causing this method to always return true.

        Specified by:
        containsKey in interface Map<K,​V>
        See Also:
        Map.containsKey(java.lang.Object)
      • getNewExpirationDate

        protected Date getNewExpirationDate()
        Returns a new Date object offset from the current date by the default expiration duration in seconds
        Returns:
        The date of expiration starting now
      • getSecondsRemaining

        public double getSecondsRemaining​(K key)
        Return the number of seconds remaining for the cache entry of the given key.

        If the key is not present or the cache entry has expired, this method returns 0.0.

        Parameters:
        key - The key to check
        Returns:
        The number of seconds remaining
      • invalidateRecords

        public void invalidateRecords()
        Invalidate all records in the internal storage that have expired
      • putAllInternal

        protected void putAllInternal​(CacheMap<? extends K,​? extends V> other)
        Puts all cache entries from another CacheMap into this one.

        They will be copied as CacheEntry objects, thus retaining their expiration timestamps. Expired entries will not be copied.

        Parameters:
        other - The other CacheMap object
      • size

        public int size()
        Returns the size of the cache, after excluding expired records.
        Specified by:
        size in interface Map<K,​V>
        See Also:
        Map.size()
      • snapshot

        public HashMap<K,​Vsnapshot()
        Returns a snapshot of this cache at this moment in time as a java.util.HashMap.

        The snapshot will not be updated to reflect any future expirations of cache data.

        Returns:
        A snapshot of this cache