public class FastReadCache<K,V> extends Object implements Cache<K,V>, Serializable, Cloneable
CacheMap that the possibility to have cache hit is much more than
not. It maintains a readonly cache (so no need to synchronize), and then
clone and replace it if there is a miss.
Thus, as time goes, most access can go directly to the readonly cache
without any synchronization or cloning.
Thread safe.
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE| Constructor and Description |
|---|
FastReadCache()
Constructor.
|
FastReadCache(int maxSize,
int lifetime)
Constructor.
|
FastReadCache(int maxSize,
int lifetime,
short maxMissCount)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears all objects being cached.
|
boolean |
containsKey(Object key)
Returns whether the specified key is stored.
|
V |
get(Object key)
Returns the object of the specified key, or null if not found.
|
int |
getLifetime()
Returns the minimal lifetime, unit=milliseconds.
|
int |
getMaxSize()
Returns the maximal allowed size.
|
V |
put(K key,
V value)
Stores an object to the cache.
|
V |
remove(Object key)
Removes an object from the cache.
|
void |
setLifetime(int lifetime)
Sets the minimal lifetime.
|
void |
setMaxSize(int maxsize)
Sets the maximal allowed size.
|
public FastReadCache()
public FastReadCache(int maxSize,
int lifetime)
public FastReadCache(int maxSize,
int lifetime,
short maxMissCount)
maxMissCount - a short value from 0 to this for sync the read cache,
default is 100.public boolean containsKey(Object key)
CachecontainsKey in interface Cache<K,V>public V get(Object key)
Cachepublic V put(K key, V value)
Cachepublic V remove(Object key)
Cachepublic void clear()
Cachepublic int getLifetime()
CachegetLifetime in interface Cache<K,V>Cache.getMaxSize()public void setLifetime(int lifetime)
CacheCache.DEFAULT_LIFETIME.setLifetime in interface Cache<K,V>lifetime - the lifetime, unit=milliseconds;
if non-positive, they will be removed immediately.Cache.getLifetime()public int getMaxSize()
CacheCache.DEFAULT_MAX_SIZE.
An mapping won't be removed by GC unless the minimal lifetime
or the maximal allowed size exceeds.getMaxSize in interface Cache<K,V>Cache.getLifetime()public void setMaxSize(int maxsize)
CachesetMaxSize in interface Cache<K,V>Cache.getMaxSize()Copyright © 2018. All rights reserved.