public class RongCache<K,V>
extends java.lang.Object
Constructor and Description |
---|
RongCache(int maxSize) |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
protected V |
create(K key)
Called after a cache miss to compute a value for the corresponding key.
|
int |
createCount()
Returns the number of times
create(Object) returned a value. |
protected void |
entryRemoved(boolean evicted,
K key,
V oldValue,
V newValue)
Called for entries that have been evicted or removed.
|
void |
evictAll()
Clear the cache, calling
entryRemoved(boolean, K, V, V) on each removed entry. |
int |
evictionCount()
Returns the number of values that have been evicted.
|
V |
get(K key)
Returns the value for
key if it exists in the cache or can be
created by #create . |
int |
hitCount()
Returns the number of times
get(K) returned a value that was
already present in the cache. |
int |
maxSize()
For caches that do not override
sizeOf(K, V) , this returns the maximum
number of entries in the cache. |
int |
missCount()
Returns the number of times
get(K) returned null or required a new
value to be created. |
V |
put(K key,
V value)
Caches
value for key . |
int |
putCount()
Returns the number of times
put(K, V) was called. |
V |
remove(K key)
Removes the entry for
key if it exists. |
void |
resize(int maxSize)
Sets the size of the cache.
|
int |
size()
For caches that do not override
sizeOf(K, V) , this returns the number
of entries in the cache. |
protected int |
sizeOf(K key,
V value)
Returns the size of the entry for
key and value in
user-defined units. |
java.util.Map<K,V> |
snapshot()
Returns a copy of the current contents of the cache, ordered from least
recently accessed to most recently accessed.
|
java.lang.String |
toString() |
public RongCache(int maxSize)
maxSize
- for caches that do not override sizeOf(K, V)
, this is
the maximum number of entries in the cache. For all other caches,
this is the maximum sum of the sizes of the entries in this cache.public void resize(int maxSize)
maxSize
- The new maximum size.public final V get(K key)
key
if it exists in the cache or can be
created by #create
. If a value was returned, it is moved to the
head of the queue. This returns null if a value is not cached and cannot
be created.public final V put(K key, V value)
value
for key
. The value is moved to the head of
the queue.key
.public final V remove(K key)
key
if it exists.key
.public void clear()
protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue)
remove(K)
, or replaced by a call to put(K, V)
. The default
implementation does nothing.
The method is called without synchronization: other threads may access the cache while this method is executing.
protected V create(K key)
The method is called without synchronization: other threads may access the cache while this method is executing.
If a value for key
exists in the cache when this method
returns, the created value will be released with entryRemoved(boolean, K, V, V)
and discarded. This can occur when multiple threads request the same key
at the same time (causing multiple values to be created), or when one
thread calls put(K, V)
while another is creating a value for the same
key.
protected int sizeOf(K key, V value)
key
and value
in
user-defined units. The default implementation returns 1 so that size
is the number of entries and max size is the maximum number of entries.
An entry's size must not change while it is in the cache.
public final void evictAll()
entryRemoved(boolean, K, V, V)
on each removed entry.public final int size()
sizeOf(K, V)
, this returns the number
of entries in the cache. For all other caches, this returns the sum of
the sizes of the entries in this cache.public final int maxSize()
sizeOf(K, V)
, this returns the maximum
number of entries in the cache. For all other caches, this returns the
maximum sum of the sizes of the entries in this cache.public final int hitCount()
get(K)
returned a value that was
already present in the cache.public final int missCount()
get(K)
returned null or required a new
value to be created.public final int createCount()
create(Object)
returned a value.public final int putCount()
put(K, V)
was called.public final int evictionCount()
public final java.util.Map<K,V> snapshot()
public final java.lang.String toString()
toString
in class java.lang.Object