Package org.jboss.util
Class TimedCachePolicy
- java.lang.Object
-
- java.util.TimerTask
-
- org.jboss.util.TimedCachePolicy
-
- All Implemented Interfaces:
java.lang.Runnable,CachePolicy
public class TimedCachePolicy extends java.util.TimerTask implements CachePolicy
An implementation of a timed cache. This is a cache whose entries have a limited lifetime with the ability to refresh their lifetime. The entries managed by the cache implement the TimedCachePolicy.TimedEntry interface. If an object inserted into the cache does not implement this interface, it will be wrapped in a DefaultTimedEntry and will expire without the possibility of refresh after getDefaultLifetime() seconds. This is a lazy cache policy in that objects are not checked for expiration until they are accessed.- Version:
- $Revision$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTimedCachePolicy.DefaultTimedEntryThe default implementation of TimedEntry used to wrap non-TimedEntry objects inserted into the cache.private classTimedCachePolicy.ResolutionTimerstatic interfaceTimedCachePolicy.TimedEntryThe interface that cache entries support.
-
Field Summary
Fields Modifier and Type Field Description protected intdefaultLifetimeThe lifetime in seconds to use for objects inserted that do not implement the TimedEntry interface.protected java.util.MapentryMapThe map of cached TimedEntry objects.private static org.jboss.logging.Loggerlogprotected longnowThe caches notion of the current timeprotected intresolutionThe resolution in seconds of the cach current timeprotected static java.util.TimerresolutionTimerprotected TimedCachePolicy.ResolutionTimertheTimerprotected booleanthreadSafeA flag indicating if entryMap should be synchronizedstatic java.lang.StringTIMER_CLASSLOADER_CONTEXTValue forTIMER_CLASSLOADER_PROPERTYindicating thethread context classloaderin effect when this class is loaded should be used.static java.lang.StringTIMER_CLASSLOADER_CURRENTValue forTIMER_CLASSLOADER_PROPERTYindicating theclassloader that loaded this classshould be used.static java.lang.StringTIMER_CLASSLOADER_PROPERTYName of system property that this class consults to determine what classloader to assign to its staticTimer's thread.static java.lang.StringTIMER_CLASSLOADER_SYSTEMValue forTIMER_CLASSLOADER_PROPERTYindicating thesystem classloadershould be used.
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcreate()Initializes the cache for use.longcurrentTimeMillis()Get the cache time.voiddestroy()Clears the cache of all entries.voidflush()Remove all entries from the cache.java.lang.Objectget(java.lang.Object key)Get the cache value for key if it has not expired.intgetDefaultLifetime()Get the default lifetime of cache entries.intgetResolution()Get the frequency of the current time snapshot.java.util.ListgetValidKeys()Get the list of keys for entries that are not expired.voidinsert(java.lang.Object key, java.lang.Object value)Insert a value into the cache.java.lang.Objectpeek(java.lang.Object key)Get the cache value for key.TimedCachePolicy.TimedEntrypeekEntry(java.lang.Object key)Get the raw TimedEntry for key without performing any expiration check.voidremove(java.lang.Object key)Remove the entry associated with key and call destroy on the entry if found.voidrun()The TimerTask run method.voidsetDefaultLifetime(int defaultLifetime)Set the default lifetime of cache entries for new values added to the cache.voidsetResolution(int resolution)Set the cache timer resolutionintsize()voidstart()Schedules this with the class resolutionTimer Timer object for execution every resolution seconds.voidstop()Stop cancels the resolution timer and flush()es the cache.
-
-
-
Field Detail
-
TIMER_CLASSLOADER_PROPERTY
public static final java.lang.String TIMER_CLASSLOADER_PROPERTY
Name of system property that this class consults to determine what classloader to assign to its staticTimer's thread.- See Also:
- Constant Field Values
-
TIMER_CLASSLOADER_SYSTEM
public static final java.lang.String TIMER_CLASSLOADER_SYSTEM
Value forTIMER_CLASSLOADER_PROPERTYindicating thesystem classloadershould be used. This is the default value if the system property is not set.- See Also:
- Constant Field Values
-
TIMER_CLASSLOADER_CURRENT
public static final java.lang.String TIMER_CLASSLOADER_CURRENT
Value forTIMER_CLASSLOADER_PROPERTYindicating theclassloader that loaded this classshould be used.- See Also:
- Constant Field Values
-
TIMER_CLASSLOADER_CONTEXT
public static final java.lang.String TIMER_CLASSLOADER_CONTEXT
Value forTIMER_CLASSLOADER_PROPERTYindicating thethread context classloaderin effect when this class is loaded should be used.- See Also:
- Constant Field Values
-
log
private static final org.jboss.logging.Logger log
-
resolutionTimer
protected static java.util.Timer resolutionTimer
-
entryMap
protected java.util.Map entryMap
The map of cached TimedEntry objects.
-
defaultLifetime
protected int defaultLifetime
The lifetime in seconds to use for objects inserted that do not implement the TimedEntry interface.
-
threadSafe
protected boolean threadSafe
A flag indicating if entryMap should be synchronized
-
now
protected long now
The caches notion of the current time
-
resolution
protected int resolution
The resolution in seconds of the cach current time
-
theTimer
protected TimedCachePolicy.ResolutionTimer theTimer
-
-
Constructor Detail
-
TimedCachePolicy
public 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
public 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.- Parameters:
defaultLifetime-
-
TimedCachePolicy
public 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.- Parameters:
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.
-
-
Method Detail
-
create
public void create()
Initializes the cache for use. Prior to this the cache has no store.- Specified by:
createin interfaceCachePolicy
-
start
public void start()
Schedules this with the class resolutionTimer Timer object for execution every resolution seconds.- Specified by:
startin interfaceCachePolicy
-
stop
public void stop()
Stop cancels the resolution timer and flush()es the cache.- Specified by:
stopin interfaceCachePolicy
-
destroy
public void destroy()
Clears the cache of all entries.- Specified by:
destroyin interfaceCachePolicy
-
get
public java.lang.Object get(java.lang.Object key)
Get the cache value for key if it has not expired. If the TimedEntry is expired its destroy method is called and then removed from the cache.- Specified by:
getin interfaceCachePolicy- Parameters:
key- the key paired with the object- Returns:
- the TimedEntry value or the original value if it was not an instance of TimedEntry if key is in the cache, null otherwise.
- See Also:
CachePolicy.peek(java.lang.Object)
-
peek
public java.lang.Object peek(java.lang.Object key)
Get the cache value for key. This method does not check to see if the entry has expired.- Specified by:
peekin interfaceCachePolicy- Parameters:
key- the key paired with the object- Returns:
- the TimedEntry value or the original value if it was not an instancee of TimedEntry if key is in the cache, null otherwise.
- See Also:
CachePolicy.get(java.lang.Object)
-
insert
public void insert(java.lang.Object key, java.lang.Object value)Insert a value into the cache. In order to have the cache entry reshresh itself value would have to implement TimedEntry and implement the required refresh() method logic.- Specified by:
insertin interfaceCachePolicy- Parameters:
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.- See Also:
CachePolicy.remove(java.lang.Object)
-
remove
public void remove(java.lang.Object key)
Remove the entry associated with key and call destroy on the entry if found.- Specified by:
removein interfaceCachePolicy- Parameters:
key- the key paired with the object- See Also:
CachePolicy.insert(java.lang.Object, java.lang.Object)
-
flush
public void flush()
Remove all entries from the cache.- Specified by:
flushin interfaceCachePolicy
-
size
public int size()
- Specified by:
sizein interfaceCachePolicy- Returns:
- the size of the cache
-
getValidKeys
public java.util.List getValidKeys()
Get the list of keys for entries that are not expired.- Returns:
- A List of the keys corresponding to valid entries
-
getDefaultLifetime
public int getDefaultLifetime()
Get the default lifetime of cache entries.- Returns:
- default lifetime in seconds of cache entries.
-
setDefaultLifetime
public void setDefaultLifetime(int defaultLifetime)
Set the default lifetime of cache entries for new values added to the cache.- Parameters:
defaultLifetime- - lifetime in seconds of cache values that do not implement TimedEntry.
-
getResolution
public int getResolution()
Get the frequency of the current time snapshot.- Returns:
- the current timer resolution in seconds.
-
setResolution
public void setResolution(int resolution)
Set the cache timer resolution- Parameters:
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.
-
run
public void run()
The TimerTask run method. It updates the cache time to the current system time.- Specified by:
runin interfacejava.lang.Runnable- Specified by:
runin classjava.util.TimerTask
-
currentTimeMillis
public long currentTimeMillis()
Get the cache time.- Returns:
- the cache time last obtained from System.currentTimeMillis()
-
peekEntry
public TimedCachePolicy.TimedEntry peekEntry(java.lang.Object key)
Get the raw TimedEntry for key without performing any expiration check.- Parameters:
key-- Returns:
- the TimedEntry value associated with key if one exists, null otherwise.
-
-