Class LRUCache<K,V>
- All Implemented Interfaces:
Map<K,
V>
Map
interface for convenience.
This class offers two implementation strategies: a locking approach and a threaded approach.
- The Locking strategy can be selected by using the constructor that takes only an int for capacity, or by using the constructor that takes an int and a StrategyType enum (StrategyType.LOCKING).
- The Threaded strategy can be selected by using the constructor that takes an int and a StrategyType enum (StrategyType.THREADED). Another constructor allows specifying a cleanup delay time.
The Locking strategy allows for O(1) access for get(), put(), and remove(). For put(), remove(), and many other methods, a write-lock is obtained. For get(), it attempts to lock but does not lock unless it can obtain it right away. This 'try-lock' approach ensures that the get() API is never blocking, but it also means that the LRU order is not perfectly maintained under heavy load.
The Threaded strategy allows for O(1) access for get(), put(), and remove() without blocking. It uses a ConcurrentHashMapNullSafe
internally. To ensure that the capacity is honored, whenever put() is called, a thread (from a thread pool) is tasked
with cleaning up items above the capacity threshold. This means that the cache may temporarily exceed its capacity, but
it will soon be trimmed back to the capacity limit by the scheduled thread.
LRUCache supports null
for both key and value.
Special Thanks: This implementation was inspired by insights and suggestions from Ben Manes.
- 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. - See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionLRUCache
(int capacity) Create a "locking-based" LRUCache with the passed in capacity.LRUCache
(int capacity, int cleanupDelayMillis) Create a "thread-based" LRUCache with the passed in capacity.LRUCache
(int capacity, LRUCache.StrategyType strategyType) Create a "locking-based" or a "thread-based" LRUCache with the passed in capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
boolean
Retrieve a value from the cache.int
int
hashCode()
boolean
isEmpty()
keySet()
Insert a value into the cache.void
Copy all of the mappings from the specified map to this cache.void
shutdown()
Deprecated.int
size()
toString()
values()
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
-
LRUCache
public LRUCache(int capacity) Create a "locking-based" LRUCache with the passed in capacity.- Parameters:
capacity
- int maximum number of entries in the cache.- See Also:
-
LRUCache
Create a "locking-based" or a "thread-based" LRUCache with the passed in capacity.- Parameters:
capacity
- int maximum number of entries in the cache.strategyType
- StrategyType.LOCKING or StrategyType.THREADED indicating the underlying LRUCache implementation.- See Also:
-
LRUCache
public LRUCache(int capacity, int cleanupDelayMillis) Create a "thread-based" LRUCache with the passed in capacity.- Parameters:
capacity
- int maximum number of entries in the cache.cleanupDelayMillis
- int number of milliseconds after a put() call when a scheduled task should run to trim the cache to no more than capacity. The default is 10ms.- See Also:
-
-
Method Details
-
getCapacity
public int getCapacity()- Returns:
- the maximum number of entries in the cache.
-
get
Retrieve a value from the cache. -
put
Insert a value into the cache. -
putAll
Copy all of the mappings from the specified map to this cache. -
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>
-
entrySet
-
keySet
-
values
-
toString
-
hashCode
public int hashCode() -
equals
-
shutdown
Deprecated.This method is no longer needed as the ThreadedLRUCacheStrategy will automatically end because it uses a daemon thread.
-