public class LRUCache extends Object implements Cache
An implementation of a Map which has a maximum size and uses a Least Recently Used algorithm to remove items from the Map when the maximum size is reached and new items are added.
A synchronized version can be obtained with:
Collections.synchronizedMap( theMapToSynchronize )
If it will be accessed by
multiple threads, you _must_ synchronize access to this Map. Even concurrent get(Object)
operations produce indeterminate behavior.
Unlike the Collections 1.0 version, this version of LRUMap does use a true LRU algorithm. The
keys for all gets and puts are moved to the front of the list. LRUMap is now a subclass of
SequencedHashMap, and the "LRU" key is now equivalent to LRUMap.getFirst().
The contents of the cache may be displayed for debugging purposes using a status frame. This
status frame can be activated by setting the system property
jadice.viewer.show-cache-status=true
.
Constructor and Description |
---|
LRUCache()
Default constructor, primarily for the purpose of de-externalization.
|
LRUCache(int maxCapacity)
Create a new LRUMap with a maximum capacity of i.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map (optional operation).
|
Object |
get(Object key)
Get the value for a key from the Map.
|
Object |
get(Object owner,
Object key)
Get the value for a key from the Map.
|
int |
getExpiryObjectCountRatio()
Returns a ratio defining the minimum number of objects to expire.
|
int |
getMaximumObjectCount()
Return the maximum mumber of entries in the cache.
|
long |
getMemoryCheckThreshold()
Returns a threshold which initiates an emergency cache clean if the VM will run out of memory.
|
int |
getMinimumExpiryAge()
Return the minimum age (in ms) of objects to be purged from the cache.
|
int |
getObjectCount()
Returns the number of objects in the cache.
|
long |
getSizeEstimate()
Returns the estimated size of all objects in the cache.
|
long |
getSizeHighwaterMark()
Returns the high water mark.
|
int |
getSizeLowwaterPercent()
Returns the low water mark.
|
SizeManagementStrategy |
getSizeManagementStrategy() |
boolean |
isEnableSoftReferences() |
boolean |
isMemoryCheckEnabled()
Returns whether the cache forces expiry runs when memory is low.
|
protected void |
performExpiry()
Purge entries from the cache until the memory use is below the low-water mark again.
|
protected void |
processRemovedEntry(Object key,
Object value)
Subclasses of LRUCache may hook into this method to provide specialized actions whenever an
Object is automatically removed from the cache.
|
Object |
put(Object key,
Object value,
int sizeEstimate)
Put the entry into the cache.
|
Object |
put(Object owner,
Object key,
Object value,
int sizeEstimate)
Store an element in the cache, keeping a weak reference to the owner, but a hard reference to
the key.
|
Object |
remove(Object key)
Remove an entry from the cache using the associated key.
|
Object |
remove(Object owner,
Object key) |
void |
scheduleExpiry()
Schedules an expiry process if required.
|
void |
setEnableSoftReferences(boolean enableSoftReferences) |
void |
setExpiryObjectCountRatio(int expiryObjectCountRatio)
Sets the expiry object count ratio.
|
void |
setMaximumObjectCount(int maximumSize)
Set the maximum mumber of entries in the cache.
|
void |
setMemoryCheckEnabled(boolean memoryCheckEnabled)
Changes the cache behavior whether the cache should force expiry runs when memory is low.
|
void |
setMemoryCheckThreshold(long memoryCheckThreshold)
Sets a threshold which initiates an emergency cache clean if the VM will run out of memory.
|
void |
setMinimumExpiryAge(int minimumExpiryAge)
Set the minimum age (in ms) of objects to be purged from the cache.
|
void |
setSizeHighwaterMark(long sizeHighwaterMark)
Sets the high water mark.
|
void |
setSizeLowwaterPercent(int sizeLowwaterPercent)
Sets the low water mark.
|
void |
setSizeManagementStrategy(SizeManagementStrategy sizeManagementStrategy) |
void |
showStatusFrame() |
public LRUCache()
public LRUCache(int maxCapacity)
maxCapacity
- Maximum capacity of the LRUMappublic void showStatusFrame()
public Object get(Object key)
Get the value for a key from the Map. The key will be promoted to the Most Recently Used position. Note that get(Object) operations will modify the underlying Collection. Calling get(Object) inside of an iteration over keys, values, etc. is currently unsupported.
get
in interface BasicCache
key
- Key to retrievepublic Object get(Object owner, Object key)
Get the value for a key from the Map. The key will be promoted to the Most Recently Used position. Note that get(Object) operations will modify the underlying Collection. Calling get(Object) inside of an iteration over keys, values, etc. is currently unsupported.
public long getSizeEstimate()
public int getObjectCount()
public Object put(Object key, Object value, int sizeEstimate)
BasicCache
put
in interface BasicCache
key
- key to which value will be associated.value
- the value to which the given key is associated.sizeEstimate
- an estimated size of the value in bytes.null
if
there was no mapping for key. A null
return can also
indicate that the map previously associated null
with
the specified key, if the implementation supports null
valuesMap.put(Object, Object)
public Object put(Object owner, Object key, Object value, int sizeEstimate)
put
in interface Cache
Cache.put(Object, Object, Object, int)
public Object remove(Object key)
put(Object key, Object value, int)
, there is a 1:1 association between key and entry.
If the entry was stored using put(Object owner, Object key, Object value, int)
however, the key specified in the call corresponds to the owner, and consequently all
entries cached by the owner are removed.remove
in interface BasicCache
key
- key whose mapping is to be removed from the map.null
if
there was no mapping for key.BasicCache.remove(java.lang.Object)
public void clear()
Cache
protected void performExpiry()
public int getMaximumObjectCount()
public void setMaximumObjectCount(int maximumSize)
maximumSize
- New value of property maximumObjectCount.public void scheduleExpiry()
public int getMinimumExpiryAge()
public void setMinimumExpiryAge(int minimumExpiryAge)
minimumExpiryAge
- The minimumExpiryAge to setpublic long getSizeHighwaterMark()
public void setSizeHighwaterMark(long sizeHighwaterMark)
sizeHighwaterMark
- The sizeHighwaterMark to setpublic int getSizeLowwaterPercent()
public void setSizeLowwaterPercent(int sizeLowwaterPercent)
sizeLowwaterPercent
- The sizeLowwaterPercent to setpublic int getExpiryObjectCountRatio()
0
, it will be ignored by expiry process.public void setExpiryObjectCountRatio(int expiryObjectCountRatio)
0
, it will be ignored by expiry process.expiryObjectCountRatio
- The expiry object count ration to setpublic boolean isMemoryCheckEnabled()
true
when expiry will be triggered otherwise
false
public void setMemoryCheckEnabled(boolean memoryCheckEnabled)
memoryCheckEnabled
- true
when expiry should be triggered otherwise
false
public long getMemoryCheckThreshold()
maxMemory - totalMemory + freeMemory < threshold
becomes true
.
If isMemoryCheckEnabled()
is false
this setting will be ignored.public void setMemoryCheckThreshold(long memoryCheckThreshold)
maxMemory - totalMemory + freeMemory < threshold
becomes true
.
If isMemoryCheckEnabled()
is false
this setting will be ignored.memoryCheckThreshold
- the heap size limit used to initiate an emergency cache clean This
setting must be at least 10485760 = 10MB or greater. If the value of
memoryCheckThreshold
is smaller then 10MB a default value of
26214400=25MB will be assumed.protected void processRemovedEntry(Object key, Object value)
BufferedImage
: calls Image.flush()
Flushable
: calls Flushable.flush()
Collection
: flushes Flushable
s contained in the collection, then clears it
key
- key that was removedvalue
- value of that key (can be null)public void setEnableSoftReferences(boolean enableSoftReferences)
public boolean isEnableSoftReferences()
public SizeManagementStrategy getSizeManagementStrategy()
public void setSizeManagementStrategy(SizeManagementStrategy sizeManagementStrategy)
Copyright © 2024 levigo holding gmbh. All rights reserved.