public class SimpleObjCache<K,V> extends Object
Eviction is based on usage. Every time a value is retrieved from the cache its 'usage timestamp' will change. When the cache is full, the value with the oldest usage timestamp will be evicted.
This class is thread-safe and is very efficient for read operations
get). The write operation (
put) will - if the cache is full
- have performance which degrades linearly with the size of the cache as all
elements will have to be inspected to find the eviction candidate. For this
reason, the class is best suited for smaller caches (say less than 1000
|Constructor and Description|
Constructs a new cache with
|Modifier and Type||Method and Description|
Removes all elements from the cache.
Returns the value to which the specified key is mapped, or
Gets the current number of elements in the cache.
Puts a value into the cache mapped to the specified key.
public SimpleObjCache(int maxSize)
maxSizecapacity. When the cache is full, the value used/created the furthest in the past will be evicted to make room for a new element.
maxSize- capacity - any value larger than zero
public V put(K key, V value)
key- key with which the specified value is to be associated
value- value to be associated with the specified key
public V get(K key)
nullif this cache contains no mapping for the key.
key- the key whose associated value is to be returned
nullif this map contains no mapping for the key
public int getCacheSize()
public void clear()