K
- keyV
- valuepublic class SimpleObjCache<K,V> extends Object
Map
.
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
elements).
Constructor and Description |
---|
SimpleObjCache(int maxSize)
Constructs a new cache with
maxSize capacity. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all elements from the cache.
|
V |
get(K key)
Returns the value to which the specified key is mapped, or
null
if this cache contains no mapping for the key. |
int |
getCacheSize()
Gets the current number of elements in the cache.
|
V |
put(K key,
V value)
Puts a value into the cache mapped to the specified key.
|
public SimpleObjCache(int maxSize)
maxSize
capacity. 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 zeropublic V put(K key, V value)
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keypublic V get(K key)
null
if this cache contains no mapping for the key.key
- the key whose associated value is to be returnednull
if this map contains no mapping for the keypublic int getCacheSize()
public void clear()