OutOfMemoryError
. This class also makes use of a timer thread which
stays running for the duration of the JVM, which is not appropriate
for use at application runtime. The map implementations
provided by the Maps
are typically a better
solution, especially the memory sensitive implementations based on weak or
soft references.public final class TimedCache<K,V>
extends java.lang.Object
TimedCache
is an implementation of a cache that
is based around an expiration timer. The underlying data store
of the cache is a HashMap
, so objects are stored
and retrieved from the cache using a (preferably immutable) key.
Note that this implementation does not permit null keys. Any use
of a null key will result in a NullPointerException
being thrown.
When storing an object in the cache, an expiration delay should
be specified which indicates how long that object will be stored
in the cache, without being accessed, before it is removed from
the cache. The default expiration time, if none is specified,
is 10 minutes per object. As long as the object is accessed,
via the get()
method, the object will remain in
the cache. Expiration times can be specified on a per-object
basis. The value NEVER_EXPIRES
, as the name implies,
can also be specified to prevent expiration of the object.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_EXPIRATION
Deprecated.
The default expiration (in minutes) for an instance of a timer
cache that was created without a specified expiration.
|
static int |
MAXIMUM_EXPIRATION
Deprecated.
The maximum expiration (in minutes) that can be set on any
object or as the default.
|
static int |
NEVER_EXPIRES
Deprecated.
The constant to specify in place of an expiration time to
indicate that the given object should never expire.
|
Constructor and Description |
---|
TimedCache()
Deprecated.
Constructs a new, empty
TimedCache instance
with default expiration on objects added to the cache. |
TimedCache(int defaultExpiration)
Deprecated.
Constructs a new, empty
TimedCache instance
with the specified default expiration on objects added to the cache
without an expiration time. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Deprecated.
Clears the contents of this cache.
|
V |
get(K key)
Deprecated.
Retrieves a previously cached object from the cache.
|
static <K,V> TimedCache<K,V> |
newInstance()
Deprecated.
Static factory method to create a new
TimedCache instance with
the default expiration time, capacity, and load factor. |
static <K,V> TimedCache<K,V> |
newInstance(int defaultExpiration)
Deprecated.
Static factory method to create a new
TimedCache instance with
the deafult capacity and load factor. |
static <K,V> TimedCache<K,V> |
newInstance(int defaultExpiration,
int initialCapacity,
float loadFactor)
Deprecated.
Static factory method to create a new
TimedCache instance. |
void |
put(K key,
V value)
Deprecated.
Stores an object into the cache.
|
void |
put(K key,
V value,
int expiration)
Deprecated.
Stores an object into the cache.
|
V |
remove(K key)
Deprecated.
Removes the mapping for this key from the cache if present.
|
void |
setDefaultExpiration(int defaultExpiration)
Deprecated.
Changes the default expiration used by the cache for objects
stored in the cache without an explicit expiration.
|
void |
setExpiration(K key,
int expiration)
Deprecated.
Changes the expiration on a previously cached object.
|
public static final int DEFAULT_EXPIRATION
public static final int MAXIMUM_EXPIRATION
public static final int NEVER_EXPIRES
TimedCache
instance.public TimedCache()
TimedCache
instance
with default expiration on objects added to the cache.public TimedCache(int defaultExpiration)
TimedCache
instance
with the specified default expiration on objects added to the cache
without an expiration time.defaultExpiration
- the expiration (in minutes) to set by
default, or DEFAULT_EXPIRATION
for the default, or
NEVER_EXPIRES
public static <K,V> TimedCache<K,V> newInstance()
TimedCache
instance with
the default expiration time, capacity, and load factor.public static <K,V> TimedCache<K,V> newInstance(int defaultExpiration)
TimedCache
instance with
the deafult capacity and load factor.defaultExpiration
- the expiration (in minutes) to set by
default, or DEFAULT_EXPIRATION
for the default, or
NEVER_EXPIRES
public static <K,V> TimedCache<K,V> newInstance(int defaultExpiration, int initialCapacity, float loadFactor)
TimedCache
instance.defaultExpiration
- the expiration (in minutes) to set by
default, or DEFAULT_EXPIRATION
for the default, or
NEVER_EXPIRES
initialCapacity
- the initial capacity of the cacheloadFactor
- the load factor of the cachepublic V get(K key)
key
- the search key for locating the cached objectpublic void put(K key, V value)
key
- the search key for determing the storage locationvalue
- the object to storepublic void put(K key, V value, int expiration)
key
- the search key for determing the storage locationvalue
- the object to storeexpiration
- the new expiration time in minutes to set,
or DEFAULT_EXPIRATION for the default, or
NEVER_EXPIRESpublic V remove(K key)
key
- the key for looking up the cache object to removenull
if no there was no mapping for the keypublic void setDefaultExpiration(int defaultExpiration)
defaultExpiration
- the expiration (in minutes) to set by
default, or DEFAULT_EXPIRATION
for the default, or
NEVER_EXPIRES
public void setExpiration(K key, int expiration)
key
- the search key for locating the cached objectexpiration
- the new expiration time in minutes to set,
or DEFAULT_EXPIRATION for the default, or
NEVER_EXPIRESpublic void clear()