Class ThreadedLRUCacheStrategy<K,V>
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
- All Implemented Interfaces:
Closeable,AutoCloseable,Map<K,V>
Map interface for convenience.
The Threaded strategy allows for O(1) access for get(), put(), and remove() without blocking. It uses a ConcurrentHashMapNullSafe
internally. A single shared background task periodically checks all cache instances and removes the least recently
used items from any cache that exceeds its capacity.
LRUCache supports null for both key and value.
Architecture: All ThreadedLRUCacheStrategy instances share a single cleanup thread that runs periodically. Each cache registers itself via a WeakReference, allowing garbage collection of unused caches. The shared task automatically removes dead references during iteration. This design prevents task accumulation under heavy load and ensures efficient resource usage regardless of how many cache instances are created.
Lifecycle: Caches are automatically managed via WeakReferences. Calling close() or shutdown()
explicitly removes the cache from the shared cleanup task. If not explicitly closed, the cache will be cleaned up
when garbage collected.
- 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
ConstructorsConstructorDescriptionThreadedLRUCacheStrategy(int capacity) Create a ThreadedLRUCacheStrategy with the specified capacity.ThreadedLRUCacheStrategy(int capacity, int cleanupDelayMillis) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclose()Closes this cache, removing it from the shared cleanup task.computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanvoidForces an immediate cleanup of this cache.intinthashCode()booleanisEmpty()keySet()voidputIfAbsent(K key, V value) voidshutdown()Shuts down this cache, removing it from the shared cleanup task.intsize()toString()values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfPresent, forEach, getOrDefault, merge, remove, replace, replace, replaceAll
-
Constructor Details
-
ThreadedLRUCacheStrategy
public ThreadedLRUCacheStrategy(int capacity) Create a ThreadedLRUCacheStrategy with the specified capacity. A shared background task monitors all cache instances and performs cleanup as needed.- Parameters:
capacity- int maximum size for the LRU cache.
-
ThreadedLRUCacheStrategy
Deprecated.UseThreadedLRUCacheStrategy(int)instead. The cleanupDelayMillis parameter is ignored.Create a ThreadedLRUCacheStrategy with the specified capacity.Note: The cleanupDelayMillis parameter is deprecated and ignored. All cache instances now share a single cleanup task with a fixed interval.
- Parameters:
capacity- int maximum size for the LRU cache.cleanupDelayMillis- ignored (formerly: milliseconds before scheduling cleanup)
-
-
Method Details
-
shutdown
public void shutdown()Shuts down this cache, removing it from the shared cleanup task. After calling this method, the cache will no longer perform automatic cleanup. Equivalent to callingclose(). -
forceCleanup
public void forceCleanup()Forces an immediate cleanup of this cache. This is primarily for testing purposes. -
close
public void close()Closes this cache, removing it from the shared cleanup task. This method is idempotent - calling it multiple times has no additional effect.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
getCapacity
public int getCapacity()- Returns:
- the maximum number of entries in the cache.
-
get
-
put
-
putAll
-
isEmpty
public boolean isEmpty() -
remove
-
computeIfAbsent
- Specified by:
computeIfAbsentin interfaceMap<K,V>
-
putIfAbsent
- Specified by:
putIfAbsentin interfaceMap<K,V>
-
clear
public void clear() -
size
public int size() -
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
entrySet
-
keySet
-
values
-
equals
-
hashCode
public int hashCode() -
toString
-
ThreadedLRUCacheStrategy(int)instead.