public class TimedCachePolicy extends TimerTask implements CachePolicy
Modifier and Type | Class and Description |
---|---|
static interface |
TimedCachePolicy.TimedEntry
The interface that cache entries support.
|
Modifier and Type | Field and Description |
---|---|
protected int |
defaultLifetime
The lifetime in seconds to use for objects inserted
that do not implement the TimedEntry interface.
|
protected Map |
entryMap
The map of cached TimedEntry objects.
|
protected long |
now
The caches notion of the current time
|
protected int |
resolution
The resolution in seconds of the cach current time
|
protected static Timer |
resolutionTimer |
protected org.jboss.util.TimedCachePolicy.ResolutionTimer |
theTimer |
protected boolean |
threadSafe
A flag indicating if entryMap should be synchronized
|
static String |
TIMER_CLASSLOADER_CONTEXT
Value for
TIMER_CLASSLOADER_PROPERTY indicating the
thread context classloader
in effect when this class is loaded should be used. |
static String |
TIMER_CLASSLOADER_CURRENT
Value for
TIMER_CLASSLOADER_PROPERTY indicating the
classloader that loaded this class should
be used. |
static String |
TIMER_CLASSLOADER_PROPERTY
Name of system property that this class consults to determine what
classloader to assign to its static
Timer 's thread. |
static String |
TIMER_CLASSLOADER_SYSTEM
Value for
TIMER_CLASSLOADER_PROPERTY indicating the
system classloader should
be used. |
Constructor and Description |
---|
TimedCachePolicy()
Creates a new TimedCachePolicy with a default entry lifetime of 30 mins
that does not synchronized access to its policy store and uses a 60
second resolution.
|
TimedCachePolicy(int defaultLifetime)
Creates a new TimedCachePolicy with the given default entry lifetime
that does not synchronized access to its policy store and uses a 60
second resolution.
|
TimedCachePolicy(int defaultLifetime,
boolean threadSafe,
int resolution)
Creates a new TimedCachePolicy with the given default entry lifetime
that does/does not synchronized access to its policy store depending
on the value of threadSafe.
|
Modifier and Type | Method and Description |
---|---|
void |
create()
Initializes the cache for use.
|
long |
currentTimeMillis()
Get the cache time.
|
void |
destroy()
Clears the cache of all entries.
|
void |
flush()
Remove all entries from the cache.
|
Object |
get(Object key)
Get the cache value for key if it has not expired.
|
int |
getDefaultLifetime()
Get the default lifetime of cache entries.
|
int |
getResolution()
Get the frequency of the current time snapshot.
|
List |
getValidKeys()
Get the list of keys for entries that are not expired.
|
void |
insert(Object key,
Object value)
Insert a value into the cache.
|
Object |
peek(Object key)
Get the cache value for key.
|
TimedCachePolicy.TimedEntry |
peekEntry(Object key)
Get the raw TimedEntry for key without performing any expiration check.
|
void |
remove(Object key)
Remove the entry associated with key and call destroy on the entry
if found.
|
void |
run()
The TimerTask run method.
|
void |
setDefaultLifetime(int defaultLifetime)
Set the default lifetime of cache entries for new values added to the cache.
|
void |
setResolution(int resolution)
Set the cache timer resolution
|
int |
size() |
void |
start()
Schedules this with the class resolutionTimer Timer object for
execution every resolution seconds.
|
void |
stop()
Stop cancels the resolution timer and flush()es the cache.
|
cancel, scheduledExecutionTime
public static final String TIMER_CLASSLOADER_PROPERTY
Timer
's thread.public static final String TIMER_CLASSLOADER_SYSTEM
TIMER_CLASSLOADER_PROPERTY
indicating the
system classloader
should
be used. This is the default value if the system property is not set.public static final String TIMER_CLASSLOADER_CURRENT
TIMER_CLASSLOADER_PROPERTY
indicating the
classloader that loaded this class
should
be used.public static final String TIMER_CLASSLOADER_CONTEXT
TIMER_CLASSLOADER_PROPERTY
indicating the
thread context classloader
in effect when this class is loaded should be used.protected static Timer resolutionTimer
protected Map entryMap
protected int defaultLifetime
protected boolean threadSafe
protected long now
protected int resolution
protected org.jboss.util.TimedCachePolicy.ResolutionTimer theTimer
public TimedCachePolicy()
public TimedCachePolicy(int defaultLifetime)
defaultLifetime
- public TimedCachePolicy(int defaultLifetime, boolean threadSafe, int resolution)
defaultLifetime
- - the lifetime in seconds to use for objects inserted
that do not implement the TimedEntry interface.threadSafe
- - a flag indicating if the cach store should be synchronized
to allow correct operation under multi-threaded access. If true, the
cache store is synchronized. If false the cache store is unsynchronized and
the cache is not thread safe.resolution
- - the resolution in seconds of the cache timer. A cache does
not query the system time on every get() invocation. Rather the cache
updates its notion of the current time every 'resolution' seconds.public void create()
create
in interface CachePolicy
public void start()
start
in interface CachePolicy
public void stop()
stop
in interface CachePolicy
public void destroy()
destroy
in interface CachePolicy
public Object get(Object key)
get
in interface CachePolicy
key
- the key paired with the objectCachePolicy.peek(java.lang.Object)
public Object peek(Object key)
peek
in interface CachePolicy
key
- the key paired with the objectCachePolicy.get(java.lang.Object)
public void insert(Object key, Object value)
insert
in interface CachePolicy
key
- - the key for the cache entryvalue
- - Either an instance of TimedEntry that will be inserted without
change, or an abitrary value that will be wrapped in a non-refreshing
TimedEntry.CachePolicy.remove(java.lang.Object)
public void remove(Object key)
remove
in interface CachePolicy
key
- the key paired with the objectCachePolicy.insert(java.lang.Object, java.lang.Object)
public void flush()
flush
in interface CachePolicy
public int size()
size
in interface CachePolicy
public List getValidKeys()
public int getDefaultLifetime()
public void setDefaultLifetime(int defaultLifetime)
defaultLifetime
- - lifetime in seconds of cache values that do
not implement TimedEntry.public int getResolution()
public void setResolution(int resolution)
resolution
- - the resolution in seconds of the cache timer. A cache does
not query the system time on every get() invocation. Rather the cache
updates its notion of the current time every 'resolution' seconds.public void run()
public long currentTimeMillis()
public TimedCachePolicy.TimedEntry peekEntry(Object key)
key
- Copyright © 2018 JBoss by Red Hat. All rights reserved.