Class MemoryCache
- All Implemented Interfaces:
Cache
The size of this cache is unbounded. To use a bounded cache, use
the BoundedCache class.
Expired items are reaped via a background high priority thread that sleeps
for intervals between expired item checks. After every reap operation, the
reaper thread sleeps for 120 seconds (by default). This implies that the
minimum expire time for any item will be 120 seconds. This sleep time can
be changed via the setReapInterval(long) method.
ThreadSafety: This class is thread safe and can be used by multiple threads concurrently.
- Version:
- 1.0 12/29/2001
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longUseful constant for cache expiry time of eight hours (in milliseconds)static final longUseful constant for cache expiry time of five minutes (in milliseconds)static final longUseful constant of five seconds (in milliseconds)static final longUseful constant of five seconds (in milliseconds)static final longUseful constant for cache expiry time of four hours (in milliseconds)static final longUseful constant for cache expiry time of one hour (in milliseconds)static final longUseful constant for cache expiry time of 1 minute (in milliseconds)static final longUseful constant of 1 second (in milliseconds)static final longUseful constant of 1 second (in milliseconds)static final longUseful constant for cache expiry time of ten minutes (in milliseconds)static final longUseful constant of ten seconds (in milliseconds)static final longUseful constant of ten seconds (in milliseconds)static final longUseful constant for cache expiry time of thirty minutes (in milliseconds)static final longUseful constant of 30 seconds (in milliseconds)static final longUseful constant of 30 seconds (in milliseconds)static final longUseful constant for cache expiry time of two hours (in milliseconds)static final longUseful constant for cache expiry time of 2 minutes (in milliseconds) -
Constructor Summary
ConstructorsConstructorDescriptionCreates a memory cache with a system-assigned logger and name and a default ttl of 30 minutes.MemoryCache(Log log, String name, long default_ttl_millis) Instantiates this class with the specified name and logger. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Empties the entire cache by expiring and removing all objects.voidclose()Closes this cache, which makes all items in the cache unavailable.booleancontainsKey(Object key) Returns true if the cache has a valid cached item specified by the key.voidExpires immediately the object specified by the key.longexpireTimeLeft(Object key) voidAdds the specified amount of time to the object (to it's current time left) before that object will be expired.Returns the cached item specified by the key.getAll()Returns all objects in the cache.longGets the default TTL in milliseconds.longgetTimeLeft(Object key) Returns the amount of time left (in milliseconds) before the object will be expired from the cache.booleanisClosed()Returns true if this cache has been closed, false otherwise.static voidConvenience method that puts an object into the cache that expires in the default TTL.Puts the specified object into the cache, mapped with the specified key.voidsetDefaultTTL(long millis) Sets the default TTL in milliseconds.voidsetReapInterval(long interval) Sets the minimum time between expired item checks.voidsetReapThreadPriority(int priority) Sets the cache reaper thread priority.toString()
-
Field Details
-
ONE_SEC
-
ONE_SECOND
-
FIVE_SEC
-
FIVE_SECOND
-
TEN_SEC
-
TEN_SECOND
-
THIRTY_SEC
-
THIRTY_SECOND
-
ONE_MIN
Useful constant for cache expiry time of 1 minute (in milliseconds)- See Also:
-
TWO_MIN
Useful constant for cache expiry time of 2 minutes (in milliseconds)- See Also:
-
FIVE_MIN
Useful constant for cache expiry time of five minutes (in milliseconds)- See Also:
-
TEN_MIN
Useful constant for cache expiry time of ten minutes (in milliseconds)- See Also:
-
THIRTY_MIN
Useful constant for cache expiry time of thirty minutes (in milliseconds)- See Also:
-
ONE_HOUR
Useful constant for cache expiry time of one hour (in milliseconds)- See Also:
-
TWO_HOUR
Useful constant for cache expiry time of two hours (in milliseconds)- See Also:
-
FOUR_HOUR
Useful constant for cache expiry time of four hours (in milliseconds)- See Also:
-
EIGHT_HOUR
Useful constant for cache expiry time of eight hours (in milliseconds)- See Also:
-
-
Constructor Details
-
MemoryCache
Instantiates this class with the specified name and logger.- Parameters:
name- String denoting the name for this objectlogger- a reference to aLogdefault_ttl- the default time to live for objects in the cache (in milliseconds) (objects may appear to live for greater than the ttl if the the reaper interval time is greater than this value). Also note, that this is the default time and that theputmethod allows a different ttl for each individual object.
-
MemoryCache
public MemoryCache()Creates a memory cache with a system-assigned logger and name and a default ttl of 30 minutes.
-
-
Method Details
-
setReapInterval
Sets the minimum time between expired item checks. -
setReapThreadPriority
Sets the cache reaper thread priority. By default the reaper runs at MAX_PRIORITY - 1 but if there are a lot of low priority caches, setting this to a lower priority could be useful. -
containsKey
Description copied from interface:CacheReturns true if the cache has a valid cached item specified by the key. If no item is found for that key or if the item has expired, returns false.- Specified by:
containsKeyin interfaceCache- Parameters:
key- typically a String but can be any object
-
get
-
getAll
Description copied from interface:CacheReturns all objects in the cache. The returned map is implementation specific but will typically contain invalid input: '<'key,item> pairs of all objects in the cache. The item might be encapsulated in another implementation-specific class.Modification to this map will affect this cache.
-
getTimeLeft
Description copied from interface:CacheReturns the amount of time left (in milliseconds) before the object will be expired from the cache.If the object specified by the key is not found in the cache, then 0 is returned.
If the object has already expired, then 0 is returned.
If the object will never expire, then -1 is returned.
- Specified by:
getTimeLeftin interfaceCache- Parameters:
key- the key with which to find the object under consideration.
-
put
Description copied from interface:CachePuts the specified object into the cache, mapped with the specified key. The object stays in the cache for the specified amount of time, after which it is discarded. If that key already contains a value, then that value is returned by this method (and also replaced by the new value).- Specified by:
putin interfaceCache- Parameters:
key- key with which the specified value is to be associated.val- value to be associated with the specified key.expiry- time in milliseconds (from the time the object is put into the cache) before it is expired. Specify -1 for the item to never expire.
-
put
Description copied from interface:CacheConvenience method that puts an object into the cache that expires in the default TTL. The default TTL can be get/set by theCache.setDefaultTTL(long)method and subclasses are free to implement a default TTL as they see fit. If that key already contains a value, then that value is returned by this method (and also replaced by the new value). -
expireTimeLeft
-
expire
Description copied from interface:CacheExpires immediately the object specified by the key. Overrides any previous setting for that object's expire time. -
extend
Description copied from interface:CacheAdds the specified amount of time to the object (to it's current time left) before that object will be expired. Overrides any previous setting that the object may have had, such as never expiring. Specify -1 for that object to never expire. Does not work for objects that have already expired. -
close
Closes this cache, which makes all items in the cache unavailable. Any items needed for later should be taken out of the cache before closing it.Note:, it is a good idea to close the cache once it's not needed, because it releases resources, including any internal threads spawned and used by this implementation.
-
isClosed
-
setDefaultTTL
Sets the default TTL in milliseconds.- Specified by:
setDefaultTTLin interfaceCache
-
getDefaultTTL
Gets the default TTL in milliseconds.- Specified by:
getDefaultTTLin interfaceCache
-
clear
-
toString
-
main
-