Class ConcurrentLruCache<T>
- java.lang.Object
-
- org.apache.tomcat.util.collections.ConcurrentLruCache<T>
-
- Type Parameters:
T- the type of entries stored in the cache
public class ConcurrentLruCache<T> extends java.lang.ObjectA thread-safe LRU (Least Recently Used) cache with a configurable size limit. Entries beyond the limit are evicted in LRU order.
-
-
Constructor Summary
Constructors Constructor Description ConcurrentLruCache(int limit)Creates a new LRU cache with the specified size limit.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(T entry)Adds an entry to the cache.voidclear()Removes all entries from the cache.booleancontains(T entry)Checks 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.
-
-
-
Method Detail
-
add
public void add(T entry)
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
public boolean contains(T entry)
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
-
-