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:
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
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
-
get
-
remove
-
clear
public void clear() -
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
putAll
-
keySet
-
values
-
entrySet
-
equals
-
hashCode
public int hashCode() -
toString
-
shutdown
public static void shutdown()Shuts down the shared scheduler. Call this method when your application is terminating.
-