Class TTLCache<K,V>

java.lang.Object
com.cedarsoftware.util.TTLCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values
All Implemented Interfaces:
AutoCloseable, Map<K,V>

public class TTLCache<K,V> extends Object implements Map<K,V>, AutoCloseable
A cache that holds items for a specified Time-To-Live (TTL) duration. Optionally, it supports Least Recently Used (LRU) eviction when a maximum size is specified. This implementation uses sentinel values to support null keys and values in a ConcurrentHashMapNullSafe. It utilizes a single background thread to manage purging of expired entries for all cache instances.
Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface java.util.Map

    Map.Entry<K extends Object,V extends Object>
  • Constructor Summary

    Constructors
    Constructor
    Description
    TTLCache(long ttlMillis)
    Constructs a TTLCache with the specified TTL.
    TTLCache(long ttlMillis, int maxSize)
    Constructs a TTLCache with the specified TTL and maximum size.
    TTLCache(long ttlMillis, int maxSize, long cleanupIntervalMillis)
    Constructs a TTLCache with the specified TTL, maximum size, and cleanup interval.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all of the mappings from this cache.
    void
    Cancel the purge task associated with this cache instance.
    boolean
    Returns true if this cache contains a mapping for the specified key and it has not expired.
    boolean
    Returns true if this cache maps one or more keys to the specified value.
     
    boolean
    Compares the specified object with this cache for equality.
    get(Object key)
    Returns the value to which the specified key is mapped, or null if this cache contains no mapping for the key or if the entry has expired.
    int
    Returns the hash code value for this cache.
    boolean
     
    Returns the keys currently held in the cache.
    put(K key, V value)
    Associates the specified value with the specified key in this cache.
    void
    putAll(Map<? extends K,? extends V> m)
    Copies all of the mappings from the specified map to this cache.
    Removes the mapping for a key from this cache if it is present.
    static void
    Shuts down the shared scheduler.
    int
     
    Returns a string representation of this cache.
    Returns the values currently held in the cache.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TTLCache

      public TTLCache(long ttlMillis)
      Constructs a TTLCache with the specified TTL. When constructed this way, there is no LRU size limitation, and the default cleanup interval is 60 seconds.
      Parameters:
      ttlMillis - the time-to-live in milliseconds for each cache entry
    • TTLCache

      public TTLCache(long ttlMillis, int maxSize)
      Constructs a TTLCache with the specified TTL and maximum size. When constructed this way, the default cleanup interval is 60 seconds.
      Parameters:
      ttlMillis - the time-to-live in milliseconds for each cache entry
      maxSize - the maximum number of entries in the cache (-1 for unlimited)
    • TTLCache

      public TTLCache(long ttlMillis, int maxSize, long cleanupIntervalMillis)
      Constructs a TTLCache with the specified TTL, maximum size, and cleanup interval.
      Parameters:
      ttlMillis - the time-to-live in milliseconds for each cache entry
      maxSize - the maximum number of entries in the cache (-1 for unlimited)
      cleanupIntervalMillis - the cleanup interval in milliseconds for purging expired entries
  • Method Details

    • put

      public V put(K key, V value)
      Associates the specified value with the specified key in this cache. The entry will expire after the configured TTL has elapsed.
      Specified by:
      put in interface Map<K,V>
    • get

      public V get(Object key)
      Returns the value to which the specified key is mapped, or null if this cache contains no mapping for the key or if the entry has expired.
      Specified by:
      get in interface Map<K,V>
    • remove

      public V remove(Object key)
      Removes the mapping for a key from this cache if it is present.
      Specified by:
      remove in interface Map<K,V>
    • clear

      public void clear()
      Removes all of the mappings from this cache.
      Specified by:
      clear in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
      Returns:
      the number of entries currently stored
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Returns:
      true if this cache contains no key-value mappings
    • containsKey

      public boolean containsKey(Object key)
      Returns true if this cache contains a mapping for the specified key and it has not expired.
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Returns true if this cache maps one or more keys to the specified value.
      Specified by:
      containsValue in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Copies all of the mappings from the specified map to this cache.
      Specified by:
      putAll in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Returns the keys currently held in the cache.

      The returned set is a snapshot and is not backed by the cache. Changes to the set or its iterator do not modify the cache contents.

      Specified by:
      keySet in interface Map<K,V>
      Returns:
      a snapshot Set of the keys contained in this cache
    • values

      public Collection<V> values()
      Returns the values currently held in the cache.

      Like keySet(), this collection is a snapshot. Mutating the returned collection or its iterator will not affect the cache.

      Specified by:
      values in interface Map<K,V>
      Returns:
      a snapshot Collection of the values contained in this cache
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      a Set view of the mappings contained in this cache
    • equals

      public boolean equals(Object o)
      Compares the specified object with this cache for equality.
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Returns the hash code value for this cache.
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a string representation of this cache.
      Overrides:
      toString in class Object
    • close

      public void close()
      Cancel the purge task associated with this cache instance.
      Specified by:
      close in interface AutoCloseable
    • shutdown

      public static void shutdown()
      Shuts down the shared scheduler. Call this method when your application is terminating.