public final class MemoryCache extends Object implements 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.
Modifier and Type | Field and Description |
---|---|
static long |
EIGHT_HOUR
Useful constant for cache expiry time of eight hours (in milliseconds)
|
static long |
FIVE_MIN
Useful constant for cache expiry time of five minutes (in milliseconds)
|
static long |
FIVE_SEC
Useful constant of five seconds (in milliseconds)
|
static long |
FIVE_SECOND
Useful constant of five seconds (in milliseconds)
|
static long |
FOUR_HOUR
Useful constant for cache expiry time of four hours (in milliseconds)
|
static long |
ONE_HOUR
Useful constant for cache expiry time of one hour (in milliseconds)
|
static long |
ONE_MIN
Useful constant for cache expiry time of 1 minute (in milliseconds)
|
static long |
ONE_SEC
Useful constant of 1 second (in milliseconds)
|
static long |
ONE_SECOND
Useful constant of 1 second (in milliseconds)
|
static long |
TEN_MIN
Useful constant for cache expiry time of ten minutes (in milliseconds)
|
static long |
TEN_SEC
Useful constant of ten seconds (in milliseconds)
|
static long |
TEN_SECOND
Useful constant of ten seconds (in milliseconds)
|
static long |
THIRTY_MIN
Useful constant for cache expiry time of thirty minutes (in milliseconds)
|
static long |
THIRTY_SEC
Useful constant of 30 seconds (in milliseconds)
|
static long |
THIRTY_SECOND
Useful constant of 30 seconds (in milliseconds)
|
static long |
TWO_HOUR
Useful constant for cache expiry time of two hours (in milliseconds)
|
static long |
TWO_MIN
Useful constant for cache expiry time of 2 minutes (in milliseconds)
|
Constructor and Description |
---|
MemoryCache()
Creates 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.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Empties the entire cache by expiring and removing all objects.
|
void |
close()
Closes this cache, which makes all items in the cache unavailable.
|
boolean |
containsKey(Object key)
Returns true if the cache has a valid cached item specified by the
key.
|
void |
expire(Object key)
Expires immediately the object specified by the key.
|
long |
expireTimeLeft(Object key) |
void |
extend(Object key,
long renewTime)
Adds the specified amount of time to the object (to it's current time left)
before that object will be expired.
|
Object |
get(Object key)
Returns the cached item specified by the key.
|
Map |
getAll()
Returns all objects in the cache.
|
long |
getDefaultTTL()
Gets the default TTL in milliseconds.
|
long |
getTimeLeft(Object key)
Returns the amount of time left (in milliseconds) before the object will be
expired from the cache.
|
boolean |
isClosed()
Returns true if this cache has been closed, false otherwise.
|
static void |
main(String[] args) |
Object |
put(Object key,
Object item)
Convenience method that puts an object into the cache that expires in
the default TTL.
|
Object |
put(Object key,
Object val,
long expiry)
Puts the specified object into the cache, mapped with the specified key.
|
void |
setDefaultTTL(long millis)
Sets the default TTL in milliseconds.
|
void |
setReapInterval(long interval)
Sets the minimum time between expired item checks.
|
void |
setReapThreadPriority(int priority)
Sets the cache reaper thread priority.
|
String |
toString() |
public static final long ONE_SEC
public static final long ONE_SECOND
public static final long FIVE_SEC
public static final long FIVE_SECOND
public static final long TEN_SEC
public static final long TEN_SECOND
public static final long THIRTY_SEC
public static final long THIRTY_SECOND
public static final long ONE_MIN
public static final long TWO_MIN
public static final long FIVE_MIN
public static final long TEN_MIN
public static final long THIRTY_MIN
public static final long ONE_HOUR
public static final long TWO_HOUR
public static final long FOUR_HOUR
public static final long EIGHT_HOUR
public MemoryCache(Log log, String name, long default_ttl_millis)
name
- String denoting the name for this objectlogger
- a reference to a Log
default_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 the put
method allows a different ttl for each individual object.public MemoryCache()
public void setReapInterval(long interval)
public void setReapThreadPriority(int priority)
public boolean containsKey(Object key)
Cache
containsKey
in interface Cache
key
- typically a String but can be any objectpublic Object get(Object key)
Cache
public Map getAll()
Cache
Modification to this map will affect this cache.
public long getTimeLeft(Object key)
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.
getTimeLeft
in interface Cache
key
- the key with which to find the object under consideration.public Object put(Object key, Object val, long expiry)
Cache
put
in interface Cache
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.public Object put(Object key, Object item)
Cache
Cache.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).public long expireTimeLeft(Object key)
public void expire(Object key)
Cache
public void extend(Object key, long renewTime)
Cache
public void close()
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.
public boolean isClosed()
public void setDefaultTTL(long millis)
setDefaultTTL
in interface Cache
public long getDefaultTTL()
getDefaultTTL
in interface Cache
public void clear()
Cache