Package org.zkoss.util
Class FastReadCache<K,V>
- java.lang.Object
-
- org.zkoss.util.FastReadCache<K,V>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,Cache<K,V>
public class FastReadCache<K,V> extends java.lang.Object implements Cache<K,V>, java.io.Serializable, java.lang.Cloneable
ACacheMap
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.
- Since:
- 6.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 FastReadCache()
Constructor.FastReadCache(int maxSize, int lifetime)
Constructor.FastReadCache(int maxSize, int lifetime, short maxMissCount)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears all objects being cached.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.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.
-
-
-
Constructor Detail
-
FastReadCache
public FastReadCache()
Constructor.
-
FastReadCache
public FastReadCache(int maxSize, int lifetime)
Constructor.
-
FastReadCache
public FastReadCache(int maxSize, int lifetime, short maxMissCount)
Constructor.- Parameters:
maxMissCount
- a short value from 0 to this for sync the read cache, default is 100.- Since:
- 6.5.2
-
-
Method Detail
-
containsKey
public boolean containsKey(java.lang.Object key)
Description copied from interface:Cache
Returns whether the specified key is stored.- Specified by:
containsKey
in interfaceCache<K,V>
-
get
public V get(java.lang.Object key)
Description copied from interface:Cache
Returns the object of the specified key, or null if not found.
-
put
public V put(K key, V value)
Description copied from interface:Cache
Stores an object to the cache.
-
remove
public V remove(java.lang.Object key)
Description copied from interface:Cache
Removes an object from the cache.
-
clear
public void clear()
Description copied from interface:Cache
Clears all objects being cached.
-
getLifetime
public int getLifetime()
Description copied from interface:Cache
Returns 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:
getLifetime
in interfaceCache<K,V>
- See Also:
Cache.getMaxSize()
-
setLifetime
public void setLifetime(int lifetime)
Description copied from interface:Cache
Sets the minimal lifetime. Default:Cache.DEFAULT_LIFETIME
.- Specified by:
setLifetime
in 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:Cache
Returns 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:
getMaxSize
in interfaceCache<K,V>
- See Also:
Cache.getLifetime()
-
setMaxSize
public void setMaxSize(int maxsize)
Description copied from interface:Cache
Sets the maximal allowed size.- Specified by:
setMaxSize
in interfaceCache<K,V>
- See Also:
Cache.getMaxSize()
-
-