public class BoundedLruCache<K,V> extends LruCache<K,V>
If an entry is added when the cache is full, this implementation removes
the least recently used entry, so that cache size
is never
greater than maxSize
.
Subclasses may override the evict
method to impose a different policy for removing stale entries when
new entries are added to the cache; e.g., permit cache overflow by retaining
cache entries that cannot currently be evicted.
LruCache.Entry<K,V>
Constructor and Description |
---|
BoundedLruCache(int maxSize)
Constructs a bounded LRU cache that is initially empty.
|
Modifier and Type | Method and Description |
---|---|
protected void |
add(LruCache.Entry<K,V> entry)
Adds a new entry to this cache in response to
LruCache.put(Object, Object) . |
protected void |
evict(LruCache.Entry<K,V> entry)
Attempts to evict an existing entry from this cache in response to
request to
makeSpace . |
protected void |
makeSpace(int sizeNeeded)
Attempts to
evict
stale entries to make space as requested. |
int |
maxSize()
Returns the maximum size of this cache.
|
void |
setMaxSize(int maxSize)
Changes the maximum size of this cache.
|
public BoundedLruCache(int maxSize)
maxSize
- the maximum size of the cache (the bound)java.lang.IllegalArgumentException
- if maxSize < 1
public final int maxSize()
public final void setMaxSize(int maxSize)
makeSpace
.maxSize
- a new value for maximum size of the cachejava.lang.IllegalArgumentException
- if maxSize < 1
protected void add(LruCache.Entry<K,V> entry)
LruCache.put(Object, Object)
.
If the cache is full, this implementation attempts to makeSpace
for the new entry. The actual addition is handled by the
super implementation.
protected void makeSpace(int sizeNeeded)
evict
stale entries to make space as requested. Follows the access order,
starting from the least recently used entry.sizeNeeded
- the requested space (>= 0)protected void evict(LruCache.Entry<K,V> entry)
makeSpace
. It is permitted
for this method to remove other cache entries along with the given entry
or, if the given entry cannot currently be evicted, retain it in the cache.
This implementation invokes doRemove
.
entry
- an existing entryCopyright (c) 2014, 2018 1C-Soft LLC and others. Made available under the Eclipse Public License 2.0