Class CacheMap<K,V>
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,,V> Cache<K,V>
- Direct Known Subclasses:
ResourceCache
The criteria can be changed by overriding canExpunge(int, org.zkoss.util.CacheMap.Value<V>).
When to check the criteria can be changed by overriding
shallExpunge().
If the criteria is totally independent of GC, you could override
shallExpunge() to always return true
(rather than when GC is activated).
It is different from WeakHashMap:
- The mapping might be removed even if the key is hold somewhere (i.e., strong reachable).
- The mapping might not be removed when GC demanding memory if the criteria doesn't meet.
- It is not serializable.
Like other maps, it is not thread-safe. To get one, use java.util.Collections.synchronizedMap.
Implementation Note: there is another version of CacheMap that uses WeakReference for each value (refer to obsolete). The drawback is that all mapping will be queued and need to be examined, because GC tends to release all reference at once.
We don't use PhantomReference because it is still required to re-create the reference after enqueued.
- Author:
- tomyeh
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classThe class to hold key/value. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote the searching of the next mapping shall continue.protected static final intReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote it shall not be expunged.protected static final intReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote the searching of the next mapping shall stop.protected static final intReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote it shall be expunged.Fields inherited from interface org.zkoss.util.Cache
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionCacheMap()Constructs a cache map.CacheMap(boolean accessOrder) Constructs a cache map.CacheMap(int cap) Constructs a cache map.CacheMap(int cap, float load) Constructs a cache map.CacheMap(int cap, float load, boolean accessOrder) Constructs a cache map.CacheMap(int maxSize, int lifetime) Constructs a cache map with the specified max size and lifetime.CacheMap(int maxSize, int lifetime, boolean accessOrder) Constructs a cache map with the specified max size and lifetime. -
Method Summary
Modifier and TypeMethodDescriptionprotected intcanExpunge(int size, CacheMap.Value<V> v) Tests whether certain value is OK to expunge.voidclear()Clears all objects being cached.clone()booleancontainsKey(Object key) Returns whether the specified key is stored.booleanTests if the given key exists without trying to expunge for more memory.booleancontainsValue(Object value) entrySet()booleanintexpunge()Enforces to expunge items that exceeds the maximal allowed number or lifetime.Returns the object of the specified key, or null if not found.intGets the minimal lifetime, unit=milliseconds.intGets the maximal allowed size.getWithoutExpunge(Object key) Returns the value without trying to expunge for more memory.inthashCode()booleanisEmpty()booleanReturns whether it is empty without trying to expunge first.keySet()protected voidonExpunge(CacheMap.Value<V> v) Called when a pair of key and value having been expunged.Stores an object to the cache.voidRemoves an object from the cache.voidsetLifetime(int lifetime) Sets the minimal lifetime.voidsetMaxSize(int maxsize) Sets the maximal allowed size.protected booleanReturns whether it is time to expunge.intsize()intReturns the size without trying to expunge first.toString()values()Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
EXPUNGE_NO
protected static final int EXPUNGE_NOReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote it shall not be expunged.- See Also:
-
EXPUNGE_YES
protected static final int EXPUNGE_YESReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote it shall be expunged.- See Also:
-
EXPUNGE_CONTINUE
protected static final int EXPUNGE_CONTINUEReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote the searching of the next mapping shall continue.- See Also:
-
EXPUNGE_STOP
protected static final int EXPUNGE_STOPReturns bycanExpunge(int, org.zkoss.util.CacheMap.Value<V>)to denote the searching of the next mapping shall stop.- See Also:
-
-
Constructor Details
-
CacheMap
public CacheMap(int maxSize, int lifetime) Constructs a cache map with the specified max size and lifetime. Unlike LinkedHashMap, the default order is the access order, i.e., the order is changed once accessed, including get().- Since:
- 3.0.0
-
CacheMap
public CacheMap()Constructs a cache map. Unlike LinkedHashMap, the default order is the access order, i.e., the order is changed once accessed, including get(). -
CacheMap
public CacheMap(int cap) Constructs a cache map. Unlike LinkedHashMap, the default order is the access order, i.e., the order is changed once accessed, including get(). -
CacheMap
public CacheMap(int cap, float load) Constructs a cache map. Unlike LinkedHashMap, the default order is the access order, i.e., the order is changed once accessed, including get(). -
CacheMap
public CacheMap(boolean accessOrder) Constructs a cache map.- Parameters:
accessOrder- whether to use the access order. Specify false for the insertion order.- Since:
- 6.0.0
-
CacheMap
public CacheMap(int maxSize, int lifetime, boolean accessOrder) Constructs a cache map with the specified max size and lifetime.- Parameters:
accessOrder- whether to use the access order. Specify false for the insertion order.- Since:
- 6.0.0
-
CacheMap
public CacheMap(int cap, float load, boolean accessOrder) Constructs a cache map.- Parameters:
accessOrder- whether to use the access order. Specify false for the insertion order.- Since:
- 6.0.0
-
-
Method Details
-
onExpunge
Called when a pair of key and value having been expunged. This method is called after it is removed, so you could add it back.Default: does nothing
-
shallExpunge
protected boolean shallExpunge()Returns whether it is time to expunge. Once shallExpunge returns true, values are examined one-by-one thrucanExpunge(int, org.zkoss.util.CacheMap.Value<V>), and expunged if EXPUNGE_YES.This implementation returns true only if GC was activated. You might override it to return true, such that expunge is enforced no matter GC was activated.
-
canExpunge
Tests whether certain value is OK to expunge.Note: values are tested thru
canExpunge(int, org.zkoss.util.CacheMap.Value<V>)only ifshallExpunge()returns true.Deriving classes might override this method to return different value for different criteria.
The return value could be a combination of EXPUNGE_xxx. One of EXPUNGE_YES and EXPUNGE_NO is returned to denote whether to expunge the mapping. One of EXPUNGE_CONTINUE and EXPUNGE_STOP is returned to denote whether to continue the searching of the next mapping for expunging.
Normally, you return either (EXPUNGE_YES|EXPUNGE_CONTINUE) or (EXPUNG_NO|EXPUNGE_STOP). Notice that the mapping is queried in the last-access order. Thus, you rarely needs to return (EXPUNGE_NO|EXPUNGE_CONTINUE) unless the appropriate one might be out of this order.
This implementation compares the access time and size. It returns (EXPUNGE_YES|EXPUNGE_CONTINUE) if OK, and (EXPUNGE_NO|EXPUNGE_STOP) if not.
- Parameters:
size- the current size. It is used instead of size(), since the entry might not be removed yet (such asFastReadCache).- Returns:
- a combination of EXPUNGE_xxx
- See Also:
-
expunge
public int expunge()Enforces to expunge items that exceeds the maximal allowed number or lifetime.By default, this method is called only GC takes places.
- Returns:
- number of items left (
size()) after expunged - Since:
- 3.6.1
-
getLifetime
public int getLifetime()Gets 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:
-
setLifetime
public void setLifetime(int lifetime) Sets 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:
-
getMaxSize
public int getMaxSize()Gets 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.Notice: getMaxSize() is only a soft limit. It takes effect only if GC takes place.
- Specified by:
getMaxSizein interfaceCache<K,V> - See Also:
-
setMaxSize
public void setMaxSize(int maxsize) Sets the maximal allowed size.- Specified by:
setMaxSizein interfaceCache<K,V> - See Also:
-
isEmpty
public boolean isEmpty() -
isEmptyWithoutExpunge
public boolean isEmptyWithoutExpunge()Returns whether it is empty without trying to expunge first.- Since:
- 3.0.1
-
size
public int size() -
sizeWithoutExpunge
public int sizeWithoutExpunge()Returns the size without trying to expunge first.- Since:
- 3.0.1
-
clear
public void clear()Description copied from interface:CacheClears all objects being cached. -
remove
Description copied from interface:CacheRemoves an object from the cache. -
get
Description copied from interface:CacheReturns the object of the specified key, or null if not found. -
getWithoutExpunge
Returns the value without trying to expunge for more memory. It is useful if you want to preserve all entries. -
containsKey
Description copied from interface:CacheReturns whether the specified key is stored.- Specified by:
containsKeyin interfaceCache<K,V> - Specified by:
containsKeyin interfaceMap<K,V>
-
containsKeyWithoutExpunge
Tests if the given key exists without trying to expunge for more memory. -
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
put
Description copied from interface:CacheStores an object to the cache. -
putAll
-
entrySet
-
keySet
-
values
-
hashCode
public int hashCode() -
equals
-
toString
-
clone
-