Class NCCache
-
- All Implemented Interfaces:
public class NCCache<K, V>Modify by DragonJ on 14/11/28. Add queue for change the way of recycle.
-
-
Constructor Summary
Constructors Constructor Description NCCache(int maxSize)
-
Method Summary
Modifier and Type Method Description voidresize(int maxSize)Sets the size of the cache. final Vget(K key)Returns the value for keyif it exists in the cache or can be created by#create.final Vput(K key, V value)Caches valueforkey.final Vremove(K key)Removes the entry for keyif it exists.voidclear()voidentryRemoved(boolean evicted, K key, V oldValue, V newValue)Called for entries that have been evicted or removed. Vcreate(K key)Called after a cache miss to compute a value for the corresponding key. intsizeOf(K key, V value)Returns the size of the entry for keyandvaluein user-defined units.final voidevictAll()Clear the cache, calling entryRemoved on each removed entry. final synchronized intsize()For caches that do not override sizeOf, this returns the number of entries in the cache. final synchronized intmaxSize()For caches that do not override sizeOf, this returns the maximum number of entries in the cache. final synchronized inthitCount()Returns the number of times get returned a value that was already present in the cache. final synchronized intmissCount()Returns the number of times get returned null or required a new value to be created. final synchronized intcreateCount()Returns the number of times create returned a value. final synchronized intputCount()Returns the number of times put was called. final synchronized intevictionCount()Returns the number of values that have been evicted. final synchronized Map<K, V>snapshot()Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed. final synchronized StringtoString()-
-
Constructor Detail
-
NCCache
NCCache(int maxSize)
- Parameters:
maxSize- for caches that do not override sizeOf, this is the maximum number of entries in the cache.
-
-
Method Detail
-
resize
void resize(int maxSize)
Sets the size of the cache.
- Parameters:
maxSize- The new maximum size.
-
get
final V get(K key)
Returns the value for
keyif 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.
-
put
final V put(K key, V value)
Caches
valueforkey. The value is moved to the head of the queue.- Returns:
the previous value mapped by
key.
-
remove
final V remove(K key)
Removes the entry for
keyif it exists.- Returns:
the previous value mapped by
key.
-
clear
void clear()
-
entryRemoved
void entryRemoved(boolean evicted, K key, V oldValue, V newValue)
Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to remove, or replaced by a call to put. The default implementation does nothing.
The method is called without synchronization: other threads may access the cache while this method is executing.
-
create
V create(K key)
Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.
The method is called without synchronization: other threads may access the cache while this method is executing.
If a value for
keyexists in the cache when this method returns, the created value will be released with entryRemoved 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 while another is creating a value for the same key.
-
sizeOf
int sizeOf(K key, V value)
Returns the size of the entry for
keyandvaluein 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.
-
evictAll
final void evictAll()
Clear the cache, calling entryRemoved on each removed entry.
-
size
final synchronized int size()
For caches that do not override sizeOf, 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.
-
maxSize
final synchronized int maxSize()
For caches that do not override sizeOf, 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.
-
hitCount
final synchronized int hitCount()
Returns the number of times get returned a value that was already present in the cache.
-
missCount
final synchronized int missCount()
Returns the number of times get returned null or required a new value to be created.
-
createCount
final synchronized int createCount()
Returns the number of times create returned a value.
-
evictionCount
final synchronized int evictionCount()
Returns the number of values that have been evicted.
-
snapshot
final synchronized Map<K, V> snapshot()
Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
-
-
-
-