public class MultiCache<K,V> extends java.lang.Object implements Cache<K,V>, java.io.Serializable, java.lang.Cloneable
CacheMap
that uses multiple instanceof CacheMap
to speed up
the performance.
It creates multiple instances of CacheMap
, called
the internal caches, and then distributes the access across them.
Thus, the performance is proportional to the number of internal caches.
Thread safe.
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE
Constructor and Description |
---|
MultiCache()
Constructs a multi cache with 17 initial caches.
|
MultiCache(int nCache)
Constructs a multi cache with the specified number of internal caches.
|
MultiCache(int nCache,
int maxSize,
int lifetime)
Constructs a multi cache with the specified number of internal caches,
the max size and the lifetime.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all objects being cached.
|
java.lang.Object |
clone() |
boolean |
containsKey(java.lang.Object key)
Returns whether the specified key is stored.
|
V |
get(java.lang.Object key)
Returns the object of the specified key, or null if not found.
|
protected int |
getInnerCacheHashCode(java.lang.Object key)
Returns an integer used to identify the instance of inner caches to use.
|
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(java.lang.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 MultiCache()
public MultiCache(int nCache, int maxSize, int lifetime)
nCache
- the positive number of the internal caches.
The large the number the fast the performance.maxSize
- the maximal allowed size of each cachepublic MultiCache(int nCache)
The default lifetime is Cache.DEFAULT_LIFETIME
, and
the default maximal allowed size of each cache is
(Cache.DEFAULT_MAX_SIZE
/ 10).
nCache
- the positive number of the internal caches.
The large the number the fast the performance.public boolean containsKey(java.lang.Object key)
Cache
containsKey
in interface Cache<K,V>
public V get(java.lang.Object key)
Cache
public V put(K key, V value)
Cache
public V remove(java.lang.Object key)
Cache
public void clear()
Cache
protected int getInnerCacheHashCode(java.lang.Object key)
public int getLifetime()
Cache
getLifetime
in interface Cache<K,V>
Cache.getMaxSize()
public void setLifetime(int lifetime)
Cache
Cache.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()
Cache
Cache.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)
Cache
setMaxSize
in interface Cache<K,V>
Cache.getMaxSize()
public java.lang.Object clone()
clone
in class java.lang.Object
Copyright © 2005-2011 Potix Corporation. All Rights Reserved.