Class ConcurrentLruCache<T>
java.lang.Object
org.apache.tomcat.util.collections.ConcurrentLruCache<T>
- Type Parameters:
T- the type of entries stored in the cache
A thread-safe LRU (Least Recently Used) cache with a configurable size limit.
Entries beyond the limit are evicted in LRU order.
-
Constructor Summary
ConstructorsConstructorDescriptionConcurrentLruCache(int limit) Creates a new LRU cache with the specified size limit. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an entry to the cache.voidclear()Removes all entries from the cache.booleanChecks whether the cache contains the given entry.intgetLimit()Returns the current maximum number of entries allowed in the cache.voidsetLimit(int limit) Sets the maximum number of entries in the cache.
-
Constructor Details
-
ConcurrentLruCache
public ConcurrentLruCache(int limit) Creates a new LRU cache with the specified size limit.- Parameters:
limit- the maximum number of entries in the cache
-
-
Method Details
-
add
Adds an entry to the cache. If the cache is at its limit, the least recently used entry will be evicted.- Parameters:
entry- the entry to add
-
contains
Checks whether the cache contains the given entry. Accessing an entry through this method updates its recency.- Parameters:
entry- the entry to check- Returns:
trueif the entry is in the cache
-
clear
public void clear()Removes all entries from the cache. -
setLimit
public void setLimit(int limit) Sets the maximum number of entries in the cache. If the new limit is smaller than the current size, the least recently used entries will be evicted. A limit of 0 or less disables the cache.- Parameters:
limit- the new maximum number of entries
-
getLimit
public int getLimit()Returns the current maximum number of entries allowed in the cache.- Returns:
- the cache limit, or -1 if the cache is disabled
-