Package com.cedarsoftware.util
Class TTLCache<K,V>
java.lang.Object
com.cedarsoftware.util.TTLCache<K,V>
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Implemented Interfaces:
AutoCloseable
,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.
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionTTLCache
(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 TypeMethodDescriptionvoid
clear()
Removes all of the mappings from this cache.void
close()
Cancel the purge task associated with this cache instance.boolean
containsKey
(Object key) Returnstrue
if this cache contains a mapping for the specified key and it has not expired.boolean
containsValue
(Object value) Returnstrue
if this cache maps one or more keys to the specified value.entrySet()
boolean
Compares the specified object with this cache for equality.Returns the value to which the specified key is mapped, ornull
if this cache contains no mapping for the key or if the entry has expired.int
hashCode()
Returns the hash code value for this cache.boolean
isEmpty()
keySet()
Returns the keys currently held in the cache.Associates the specified value with the specified key in this cache.void
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
shutdown()
Shuts down the shared scheduler.int
size()
toString()
Returns a string representation of this cache.values()
Returns the values currently held in the cache.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
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 entrymaxSize
- 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 entrymaxSize
- the maximum number of entries in the cache (-1 for unlimited)cleanupIntervalMillis
- the cleanup interval in milliseconds for purging expired entries
-
-
Method Details
-
put
Associates the specified value with the specified key in this cache. The entry will expire after the configured TTL has elapsed. -
get
Returns the value to which the specified key is mapped, ornull
if this cache contains no mapping for the key or if the entry has expired. -
remove
Removes the mapping for a key from this cache if it is present. -
clear
public void clear()Removes all of the mappings from this cache. -
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
Returnstrue
if this cache contains a mapping for the specified key and it has not expired.- Specified by:
containsKey
in interfaceMap<K,
V>
-
containsValue
Returnstrue
if this cache maps one or more keys to the specified value.- Specified by:
containsValue
in interfaceMap<K,
V>
-
putAll
Copies all of the mappings from the specified map to this cache. -
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.
-
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 interfaceMap<K,
V> - Returns:
- a snapshot
Collection
of the values contained in this cache
-
entrySet
-
equals
Compares the specified object with this cache for equality. -
hashCode
public int hashCode()Returns the hash code value for this cache. -
toString
Returns a string representation of this cache. -
close
public void close()Cancel the purge task associated with this cache instance.- Specified by:
close
in interfaceAutoCloseable
-
shutdown
public static void shutdown()Shuts down the shared scheduler. Call this method when your application is terminating.
-