Package com.cedarsoftware.util.cache
Class LockingLRUCacheStrategy<K,V>
java.lang.Object
com.cedarsoftware.util.cache.LockingLRUCacheStrategy<K,V>
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Implemented Interfaces:
Map<K,
V>
This class provides a thread-safe Least Recently Used (LRU) cache API that evicts the least recently used items
once a threshold is met. It implements the
Map
interface for convenience.
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.
LRUCache supports null
for both key and value.
- 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
ConstructorsConstructorDescriptionLockingLRUCacheStrategy
(int capacity) Constructs a new LRU cache with the specified maximum capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all mappings from this cache.boolean
containsKey
(Object key) Returns true if this cache contains a mapping for the specified key.boolean
containsValue
(Object value) Returns true if this cache maps one or more keys to the specified value.entrySet()
Returns a Set view of the mappings contained in this cache.boolean
Compares the specified object with this cache for equality.Returns the value associated with the specified key in this cache.int
int
hashCode()
Returns the hash code value for this cache.boolean
isEmpty()
Returns true if this cache contains no key-value mappings.keySet()
Returns a Set view of the keys contained in this cache.Associates the specified value with the specified key in this cache.void
Copies all mappings from the specified map to this cache.Removes the mapping for the specified key from this cache if present.int
size()
Returns the number of key-value mappings in this cache.toString()
Returns a string representation of this cache.values()
Returns a Collection view of the values contained in this 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
-
LockingLRUCacheStrategy
public LockingLRUCacheStrategy(int capacity) Constructs a new LRU cache with the specified maximum capacity.- Parameters:
capacity
- the maximum number of entries the cache can hold- Throws:
IllegalArgumentException
- if capacity is negative
-
-
Method Details
-
getCapacity
public int getCapacity()- Returns:
- the maximum number of entries in the cache.
-
get
Returns the value associated with the specified key in this cache. If the key exists, attempts to move it to the front of the LRU list using a non-blocking try-lock approach. -
put
Associates the specified value with the specified key in this cache. If the cache previously contained a mapping for the key, the old value is replaced and moved to the front of the LRU list. If the cache is at capacity, removes the least recently used item before adding the new item. -
putAll
Copies all mappings from the specified map to this cache. These operations will be performed atomically under a single lock.- Specified by:
putAll
in interfaceMap<K,
V> - Parameters:
m
- mappings to be stored in this cache- Throws:
NullPointerException
- if the specified map is null
-
remove
Removes the mapping for the specified key from this cache if present. -
clear
public void clear()Removes all mappings from this cache. The cache will be empty after this call returns. -
size
public int size()Returns the number of key-value mappings in this cache. -
isEmpty
public boolean isEmpty()Returns true if this cache contains no key-value mappings. -
containsKey
Returns true if this cache contains a mapping for the specified key.- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- key whose presence in this cache is to be tested- Returns:
- true if this cache contains a mapping for the specified key
-
containsValue
Returns true if this cache maps one or more keys to the specified value. This operation requires a full traversal of the cache under a lock.- Specified by:
containsValue
in interfaceMap<K,
V> - Parameters:
value
- value whose presence in this cache is to be tested- Returns:
- true if this cache maps one or more keys to the specified value
-
entrySet
Returns a Set view of the mappings contained in this cache. The set is backed by a new LinkedHashMap to maintain the LRU order. This operation requires a full traversal under a lock. -
keySet
Returns a Set view of the keys contained in this cache. The set maintains the LRU order of the cache. This operation requires a full traversal under a lock. -
values
Returns a Collection view of the values contained in this cache. The collection maintains the LRU order of the cache. This operation requires a full traversal under a lock. -
equals
Compares the specified object with this cache for equality. Returns true if the given object is also a map and the two maps represent the same mappings. -
toString
Returns a string representation of this cache. The string representation consists of a list of key-value mappings in LRU order (most recently used first) enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", ". -
hashCode
public int hashCode()Returns the hash code value for this cache. The hash code is computed by iterating over all entries in LRU order and combining their hash codes.
-