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:
Map<K,V>

public class TTLCache<K,V> extends Object implements Map<K,V>
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.
  • 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)
      Specified by:
      put in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • shutdown

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