| java.lang.Object org.zkoss.util.CacheMap
All known Subclasses: org.zkoss.util.resource.ResourceCache,
CacheMap | public class CacheMap implements Map,Cache,java.io.Serializable,Cloneable(Code) | | The cache map. The key-to-value mappings hold in this map is
temporary. They are removed when GC demanding memory and a
criteria is met. The criteria is whether the mapping is old enough
(called lifetime), or the upper bound is hit (called max-size).
The criteria can be changed by overriding
CacheMap.canExpunge .
When to check the criteria can be changed by overriding
CacheMap.shallExpunge .
If the criteria is totally independent of GC, you could override
CacheMap.newQueue to return null. Then,
CacheMap.shallExpunge always returns true (rather than when GC is activated) -- of course,
you could override
CacheMap.shallExpunge , too.
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 |
Constructor Summary | |
public | CacheMap(int maxSize, int lifetime) Constructs a cache map with the specified max size and lifetime. | public | CacheMap() Constructs a cachemap by using LinkedHashMap internally. | public | CacheMap(int cap) Constructs a cachemap by using LinkedHashMap internally. | public | CacheMap(int cap, float load) Constructs a cachemap by using LinkedHashMap internally. |
EXPUNGE_CONTINUE | final protected static int EXPUNGE_CONTINUE(Code) | | Returns by
CacheMap.canExpunge to denote the searching of the
next mapping shall continue.
|
EXPUNGE_STOP | final protected static int EXPUNGE_STOP(Code) | | Returns by
CacheMap.canExpunge to denote the searching of the
next mapping shall stop.
|
CacheMap | public CacheMap(int maxSize, int lifetime)(Code) | | Constructs a cache map with the specified max size and lifetime.
since: 3.0.0 |
CacheMap | public CacheMap()(Code) | | Constructs a cachemap by using LinkedHashMap internally.
|
CacheMap | public CacheMap(int cap)(Code) | | Constructs a cachemap by using LinkedHashMap internally.
|
CacheMap | public CacheMap(int cap, float load)(Code) | | Constructs a cachemap by using LinkedHashMap internally.
|
canExpunge | protected int canExpunge(Value v)(Code) | | Tests whether certain value is OK to expunge.
Note: values are tested thru
CacheMap.canExpunge only if
CacheMap.shallExpunge returns true.
Deriving classes might override this method to return different
value for different criteria.
The return value coulde 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.
a combination of EXPUNGE_xxx See Also: CacheMap.shallExpunge |
clear | public void clear()(Code) | | |
clone | public Object clone()(Code) | | To make sure that it is in the acess order.
|
containsValue | public boolean containsValue(Object value)(Code) | | |
getLifetime | public int getLifetime()(Code) | | Gets the minimal lifetime, unit=milliseconds.
An mapping won't be removed by GC unless the minimal lifetime
or the maximal allowed size exceeds.
See Also: CacheMap.getMaxSize |
getWithoutExpunge | public Object getWithoutExpunge(Object key)(Code) | | Returns the value without trying to expunge first.
It is useful if you want to preserve all entries.
|
hashCode | public int hashCode()(Code) | | |
isEmpty | public boolean isEmpty()(Code) | | Gets the last accessed time, in system millisecs.
the last accessed time; 0 if not found |
isEmptyWithoutExpunge | public boolean isEmptyWithoutExpunge()(Code) | | Returns whether it is empty without trying to expunge first.
since: 3.0.1 |
newQueue | protected ReferenceQueue newQueue()(Code) | | Creates the reference queue.
It is called only once in the constructor (so it is meaningless
to change the returned value after constructed).
Default: new ReferenceQueue();
Override this method to return null if you want to expunge items
every time
CacheMap.get or
CacheMap.put is called -- not only GC
is activated.
In other words, if
CacheMap.newQueue returns null,
CacheMap.shallExpunge always returns true (unless you override it too).
|
onExpunge | protected void onExpunge(Value v)(Code) | | 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()(Code) | | Returns whether it is time to expunge.
Once shallExpunge returns true, values are examined one-by-one thru
CacheMap.canExpunge , and expunged if EXPUNGE_YES.
This implementation returns true only if
CacheMap.newQueue returns null (in constructor) or GC was activated.
You might override it to enforce expunge besides GC.
See Also: CacheMap.canExpunge |
sizeWithoutExpunge | public int sizeWithoutExpunge()(Code) | | Returns the size without trying to expunge first.
since: 3.0.1 |
|
|