public class CacheMap<K,V> extends java.lang.Object implements java.util.Map<K,V>, Cache<K,V>, java.io.Serializable, java.lang.Cloneable
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:
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.
Modifier and Type | Class and Description |
---|---|
protected static class |
CacheMap.Value<V>
The class to hold key/value.
|
Modifier and Type | Field and Description |
---|---|
protected static int |
EXPUNGE_CONTINUE
Returns by
canExpunge(int, org.zkoss.util.CacheMap.Value<V>) to denote the searching of the
next mapping shall continue. |
protected static int |
EXPUNGE_NO
Returns by
canExpunge(int, org.zkoss.util.CacheMap.Value<V>) to denote it shall not be expunged. |
protected static int |
EXPUNGE_STOP
Returns by
canExpunge(int, org.zkoss.util.CacheMap.Value<V>) to denote the searching of the
next mapping shall stop. |
protected static int |
EXPUNGE_YES
Returns by
canExpunge(int, org.zkoss.util.CacheMap.Value<V>) to denote it shall be expunged. |
DEFAULT_LIFETIME, DEFAULT_MAX_SIZE
Constructor and Description |
---|
CacheMap()
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.
|
Modifier and Type | Method and Description |
---|---|
protected int |
canExpunge(int size,
CacheMap.Value<V> v)
Tests whether certain value is OK to expunge.
|
void |
clear()
Clears all objects being cached.
|
java.lang.Object |
clone() |
boolean |
containsKey(java.lang.Object key)
Returns whether the specified key is stored.
|
boolean |
containsKeyWithoutExpunge(java.lang.Object key)
Tests if the given key exists without trying to expunge for more
memory.
|
boolean |
containsValue(java.lang.Object value) |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
boolean |
equals(java.lang.Object o) |
int |
expunge()
Enforces to expunge items that exceeds the maximal allowed number
or lifetime.
|
V |
get(java.lang.Object key)
Returns the object of the specified key, or null if not found.
|
int |
getLifetime()
Gets the minimal lifetime, unit=milliseconds.
|
int |
getMaxSize()
Gets the maximal allowed size.
|
V |
getWithoutExpunge(java.lang.Object key)
Returns the value without trying to expunge for more
memory.
|
int |
hashCode() |
boolean |
isEmpty() |
boolean |
isEmptyWithoutExpunge()
Returns whether it is empty without trying to expunge first.
|
java.util.Set<K> |
keySet() |
protected void |
onExpunge(CacheMap.Value<V> v)
Called when a pair of key and value having been expunged.
|
V |
put(K key,
V value)
Stores an object to the cache.
|
void |
putAll(java.util.Map<? extends K,? extends V> map) |
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.
|
protected boolean |
shallExpunge()
Returns whether it is time to expunge.
|
int |
size() |
int |
sizeWithoutExpunge()
Returns the size without trying to expunge first.
|
java.lang.String |
toString() |
java.util.Collection<V> |
values() |
protected static final int EXPUNGE_NO
canExpunge(int, org.zkoss.util.CacheMap.Value<V>)
to denote it shall not be expunged.protected static final int EXPUNGE_YES
canExpunge(int, org.zkoss.util.CacheMap.Value<V>)
to denote it shall be expunged.protected static final int EXPUNGE_CONTINUE
canExpunge(int, org.zkoss.util.CacheMap.Value<V>)
to denote the searching of the
next mapping shall continue.protected static final int EXPUNGE_STOP
canExpunge(int, org.zkoss.util.CacheMap.Value<V>)
to denote the searching of the
next mapping shall stop.public CacheMap(int maxSize, int lifetime)
public CacheMap()
public CacheMap(int cap)
public CacheMap(int cap, float load)
public CacheMap(boolean accessOrder)
accessOrder
- whether to use the access order.
Specify false for the insertion order.public CacheMap(int maxSize, int lifetime, boolean accessOrder)
accessOrder
- whether to use the access order.
Specify false for the insertion order.public CacheMap(int cap, float load, boolean accessOrder)
accessOrder
- whether to use the access order.
Specify false for the insertion order.protected void onExpunge(CacheMap.Value<V> v)
Default: does nothing
protected boolean shallExpunge()
canExpunge(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.
protected int canExpunge(int size, CacheMap.Value<V> v)
Note: values are tested thru canExpunge(int, org.zkoss.util.CacheMap.Value<V>)
only if
shallExpunge()
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.
size
- the current size. It is used instead of size(), since
the entry might not be removed yet (such as FastReadCache
).shallExpunge()
public int expunge()
By default, this method is called only GC takes places.
size()
) after expungedpublic int getLifetime()
getLifetime
in interface Cache<K,V>
getMaxSize()
public void setLifetime(int lifetime)
Cache.DEFAULT_LIFETIME
.setLifetime
in interface Cache<K,V>
lifetime
- the lifetime, unit=milliseconds;
if non-positive, they will be removed immediately.getLifetime()
public int getMaxSize()
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.
getMaxSize
in interface Cache<K,V>
getLifetime()
public void setMaxSize(int maxsize)
setMaxSize
in interface Cache<K,V>
getMaxSize()
public boolean isEmptyWithoutExpunge()
public int sizeWithoutExpunge()
public void clear()
Cache
public V remove(java.lang.Object key)
Cache
public V get(java.lang.Object key)
Cache
public V getWithoutExpunge(java.lang.Object key)
public boolean containsKey(java.lang.Object key)
Cache
public boolean containsKeyWithoutExpunge(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public V put(K key, V value)
Cache
public int hashCode()
public boolean equals(java.lang.Object o)
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object clone()
clone
in class java.lang.Object
Copyright © 2005-2018 Potix Corporation. All Rights Reserved.