Package org.zkoss.util
Class MultiCache<K,V>
- java.lang.Object
-
- org.zkoss.util.MultiCache<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,Cache<K,V>
public class MultiCache<K,V> extends java.lang.Object implements Cache<K,V>, java.io.Serializable, java.lang.Cloneable
ACacheMapthat uses multiple instanceofCacheMapto speed up the performance. It creates multiple instances ofCacheMap, called the internal caches, and then distributes the access across them. Thus, the performance is proportional to the number of internal caches.Thread safe.
- Since:
- 3.0.0
- Author:
- tomyeh
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from interface org.zkoss.util.Cache
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears all objects being cached.java.lang.Objectclone()booleancontainsKey(java.lang.Object key)Returns whether the specified key is stored.Vget(java.lang.Object key)Returns the object of the specified key, or null if not found.protected intgetInnerCacheHashCode(java.lang.Object key)Returns an integer used to identify the instance of inner caches to use.intgetLifetime()Returns the minimal lifetime, unit=milliseconds.intgetMaxSize()Returns the maximal allowed size.Vput(K key, V value)Stores an object to the cache.Vremove(java.lang.Object key)Removes an object from the cache.voidsetLifetime(int lifetime)Sets the minimal lifetime.voidsetMaxSize(int maxsize)Sets the maximal allowed size.
-
-
-
Constructor Detail
-
MultiCache
public MultiCache()
Constructs a multi cache with 17 initial caches.
-
MultiCache
public MultiCache(int nCache, int maxSize, int lifetime)Constructs a multi cache with the specified number of internal caches, the max size and the lifetime.- Parameters:
nCache- the positive number of the internal caches. The large the number the fast the performance.maxSize- the maximal allowed size of each cache
-
MultiCache
public MultiCache(int nCache)
Constructs a multi cache with the specified number of internal caches.The default lifetime is
Cache.DEFAULT_LIFETIME, and the default maximal allowed size of each cache is (Cache.DEFAULT_MAX_SIZE/ 10).- Parameters:
nCache- the positive number of the internal caches. The large the number the fast the performance.
-
-
Method Detail
-
containsKey
public boolean containsKey(java.lang.Object key)
Description copied from interface:CacheReturns whether the specified key is stored.- Specified by:
containsKeyin interfaceCache<K,V>
-
get
public V get(java.lang.Object key)
Description copied from interface:CacheReturns the object of the specified key, or null if not found.
-
put
public V put(K key, V value)
Description copied from interface:CacheStores an object to the cache.
-
remove
public V remove(java.lang.Object key)
Description copied from interface:CacheRemoves an object from the cache.
-
clear
public void clear()
Description copied from interface:CacheClears all objects being cached.
-
getInnerCacheHashCode
protected int getInnerCacheHashCode(java.lang.Object key)
Returns an integer used to identify the instance of inner caches to use. By default, this method returns the hash code of the given key and the current thread. It means the value of the same key might be stored in different cache (in favor of performance). If different threads of your application usually access different keys, you can override this method to return the hash code of the given key only.- Since:
- 6.0.0
-
getLifetime
public int getLifetime()
Description copied from interface:CacheReturns the minimal lifetime, unit=milliseconds. An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.- Specified by:
getLifetimein interfaceCache<K,V>- See Also:
Cache.getMaxSize()
-
setLifetime
public void setLifetime(int lifetime)
Description copied from interface:CacheSets the minimal lifetime. Default:Cache.DEFAULT_LIFETIME.- Specified by:
setLifetimein interfaceCache<K,V>- Parameters:
lifetime- the lifetime, unit=milliseconds; if non-positive, they will be removed immediately.- See Also:
Cache.getLifetime()
-
getMaxSize
public int getMaxSize()
Description copied from interface:CacheReturns the maximal allowed size. Default:Cache.DEFAULT_MAX_SIZE. An mapping won't be removed by GC unless the minimal lifetime or the maximal allowed size exceeds.- Specified by:
getMaxSizein interfaceCache<K,V>- See Also:
Cache.getLifetime()
-
setMaxSize
public void setMaxSize(int maxsize)
Description copied from interface:CacheSets the maximal allowed size.- Specified by:
setMaxSizein interfaceCache<K,V>- See Also:
Cache.getMaxSize()
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
-