Like WeakHashMap, except that the keys of the n most recently-accessed entries are kept as {@link SoftReference softreferences}. Accessing an element means creating it, or retrieving it with {@link #get(Object) get}. Because these entries are kept with soft references, they will tend to remain even if their keys are not referenced elsewhere. But if memory is short, they will be removed.
Like WeakHashMap, except that the keys of the n most recently-accessed entries are kept as {@link SoftReference softreferences}. Accessing an element means creating it, or retrieving it with {@link #get(Object) get}. Because these entries are kept with soft references, they will tend to remain even if their keys are not referenced elsewhere. But if memory is short, they will be removed.
HashMap
. Entries in the map have a limited lifelength, and the get
method will only return the stored object during a limited time period.
After the timeout period, thr object will be removed.
@see CachedObjectThe criteria can be changed by overriding {@link #canExpunge}. When to check the criteria can be changed by overriding {@link #shallExpunge}.
If the criteria is totally independent of GC, you could override {@link #newQueue} to return null. Then, {@link #shallExpunge}always returns true (rather than when GC is activated) -- of course, you could override {@link #shallExpunge}, too.
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. @author tomyeh
|
|
|
|
|
|
|
|
|
|
|
|