|
EchoPoint API - 3.0.0b5 App Webcontainer |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectechopoint.util.collections.ExpiryCache
public class ExpiryCache
ExpiryCache implements a Map cache contains
objects that "expire".
By default, soft references are used to the cached data so that they can be reclaimed in low memory conditions regardless of whether they have expired or not.
The time-to-live and access-timeout is used to decide when an object has expired and needs to be removed from the cache.
Time-to-live is simple. Once the specified period elapses, the object is removed from the cache, regardless of how many times its been accessed.
Access-timeout is a little more complicated. Each time the object is taken from the cache, its lastAccessTime is tracked. If the access-timeout has expired (since its last access) then the object is taken from the cache.
If both the time-to-live and access-timeout is -1, then the object will never expire from the cache.
The objVersion value, which can be specified at put time, can be later retrieved along with the object. This allows for application level versioning of objects against when they put in the cache. For example if you cached the contents of a File read, you might want to store the File.lastModified() in the objVersion number. Later when you want the cached contents, you could check the put time objVersion number against the current File.lastModified(). So even though the cached content has not expired, the underlying content has and hence should be re-read.
Under the cover this class uses a ConcurrentReaderHashMap and hence is thread safe for writes to the cache.
| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from interface java.util.Map |
|---|
Map.Entry<K,V> |
| Field Summary | |
|---|---|
static long |
DEFAULT_ACCESS_TIMEOUT
the default cache access time out 5 minutes |
static long |
DEFAULT_TIME_TO_LIVE
the default cache time-to-live is 60 minutes |
| Constructor Summary | |
|---|---|
ExpiryCache()
Constructs a default ExpiryCache that uses
SoftReferences |
|
ExpiryCache(long timeToLive,
long accessTimeout)
Constructs a ExpiryCache that uses
SoftReferences |
|
ExpiryCache(long timeToLive,
long accessTimeout,
boolean softReferences)
Constructs a ExpiryCache with all the parameters |
|
| Method Summary | |
|---|---|
void |
clear()
|
boolean |
containsKey(Object key)
|
boolean |
containsValue(Object value)
This operation is not supported on ExpiryCache. |
Set |
entrySet()
This operation is not supported on ExpiryCache. |
Object |
get(Object key)
Retrieves an object from the cache. |
Object |
get(Object key,
long objVersion)
Retrieves an object from the cache. |
boolean |
hasExpired(Object key,
long when)
Called to see if a cache entry has expired or not at a given point in time. |
int |
howManyTimesAccessed(Object key)
Returns the number of times the object was accessed under a given key |
boolean |
isEmpty()
Note this may return false when in fact all objects in the cache have expired. |
boolean |
isSoftReferences()
Returns true if SoftReferences are used to cached data |
Set |
keySet()
Returns a Set of all the keys in the cache. |
protected void |
onExpiredObject(Object key)
Called when an object has been detected as expired or versioned out of existence. |
Object |
put(Object key,
Object objToCache)
Places an object into the cache. |
Object |
put(Object key,
Object objToCache,
long objVersion)
Places an object into the cache. |
Object |
put(Object key,
Object objToCache,
long timeToLive,
long accessTimeout)
Places an object into the cache with the specified 'time-to-live' and a 'access time out' value. |
Object |
put(Object key,
Object objToCache,
long timeToLive,
long accessTimeout,
long objVersion)
Places an object into the cache with the specified 'time-to-live' and a 'access time out' value as well as a version number. |
void |
putAll(Map t)
|
Object |
remove(Object key)
|
void |
setAccessTimeout(long milliSecs)
Sets the default access timeout for a cache entry |
void |
setSoftReferences(boolean newValue)
Sets whether SoftReferences are used to hold cache
entries. |
void |
setTimeToLive(long milliSecs)
Sets the default 'time-to-live' for a cache entry |
int |
size()
Note this may return a size larger than the number of non expired objects. |
Collection |
values()
This operation is not supported on ExpiryCache. |
long |
whenCached(Object key)
Returns the time when the object was cached under a given key |
long |
whenLastAccessed(Object key)
Returns the time when the object was last accessed under a given key |
long |
whenVersion(Object key)
Returns the version number that was provided when the object was placed in the cache. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface java.util.Map |
|---|
equals, hashCode |
| Field Detail |
|---|
public static final long DEFAULT_TIME_TO_LIVE
public static final long DEFAULT_ACCESS_TIMEOUT
| Constructor Detail |
|---|
public ExpiryCache()
ExpiryCache that uses
SoftReferences
public ExpiryCache(long timeToLive,
long accessTimeout)
ExpiryCache that uses
SoftReferences
timeToLive - -
the default time-to-live for a cache entryaccessTimeout - -
the default access timeout for a cache entry
public ExpiryCache(long timeToLive,
long accessTimeout,
boolean softReferences)
ExpiryCache with all the parameters
timeToLive - -
the default time-to-live for a cache entryaccessTimeout - -
the default access timeout for a cache entrysoftReferences - -
whether SoftReferences are used to cached data| Method Detail |
|---|
public void setTimeToLive(long milliSecs)
milliSecs - -
'time-to-live' for a cache entrypublic void setAccessTimeout(long milliSecs)
milliSecs - -
access timeout for a cache entrypublic long whenCached(Object key)
key - -
the key to the cached object
public long whenLastAccessed(Object key)
key - -
the key to the cached object
public long whenVersion(Object key)
key - -
the key to the cached object
public int howManyTimesAccessed(Object key)
key - -
the key to the cached object
public boolean isSoftReferences()
SoftReferences are used to cached data
SoftReferences are used to cached datapublic void setSoftReferences(boolean newValue)
SoftReferences are used to hold cache
entries.
newValue - -
the new value of the flagpublic void clear()
clear in interface MapMap.clear()public boolean containsKey(Object key)
containsKey in interface MapMap.containsKey(java.lang.Object)public Object remove(Object key)
remove in interface MapMap.remove(java.lang.Object)public int size()
size in interface MapMap.size()public boolean isEmpty()
isEmpty in interface MapMap.isEmpty()public void putAll(Map t)
putAll in interface MapMap.putAll(java.util.Map)public Set keySet()
keySet in interface Mappublic Set entrySet()
entrySet in interface MapUnsupportedOperationExceptionpublic Collection values()
values in interface MapUnsupportedOperationExceptionpublic boolean containsValue(Object value)
containsValue in interface MapUnsupportedOperationException
public Object put(Object key,
Object objToCache)
put in interface Mapkey - -
the key of the cached objectobjToCache - -
the object to cache
public Object put(Object key,
Object objToCache,
long objVersion)
key - -
the key of the cached objectobjToCache - -
the object to cacheobjVersion - -
the version of the object at the time it is put
public Object put(Object key,
Object objToCache,
long timeToLive,
long accessTimeout)
key - -
the key of the cached objectobjToCache - -
the object to cachetimeToLive - -
the time-to-live on the object or -1 to live for everaccessTimeout - -
the accessTimeout on the object or -1 to never time out
public Object put(Object key,
Object objToCache,
long timeToLive,
long accessTimeout,
long objVersion)
key - -
the key of the cached objectobjToCache - -
the object to cachetimeToLive - -
the time-to-live on the object or -1 to live for everaccessTimeout - -
the accessTimeout on the object or -1 to never time outobjVersion - -
a version number that can be used later
public Object get(Object key)
get in interface Mapkey - -
the key to the cached object
public Object get(Object key,
long objVersion)
key - -
the key to the cached objectobjVersion - -
the object version number to compare against
protected void onExpiredObject(Object key)
key - -
the key to the object
public boolean hasExpired(Object key,
long when)
key - -
the key to the cached objectwhen - -
the time to do the comparision against
|
EchoPoint API - 3.0.0b5 App Webcontainer |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||