| Method from org.jboss.util.TimedCachePolicy Detail: |
public void create() {
if( threadSafe )
entryMap = Collections.synchronizedMap(new HashMap());
else
entryMap = new HashMap();
now = System.currentTimeMillis();
}
Initializes the cache for use. Prior to this the cache has no store. |
public long currentTimeMillis() {
return now;
}
|
public void destroy() {
entryMap.clear();
}
Clears the cache of all entries. |
public void flush() {
Map tmpMap = null;
synchronized( this )
{
tmpMap = entryMap;
if( threadSafe )
entryMap = Collections.synchronizedMap(new HashMap());
else
entryMap = new HashMap();
}
// Notify the entries of their removal
Iterator iter = tmpMap.values().iterator();
while( iter.hasNext() )
{
TimedEntry entry = (TimedEntry) iter.next();
entry.destroy();
}
tmpMap.clear();
}
Remove all entries from the cache. |
public Object get(Object key) {
TimedEntry entry = (TimedEntry) entryMap.get(key);
if( entry == null )
return null;
if( entry.isCurrent(now) == false )
{ // Try to refresh the entry
if( entry.refresh() == false )
{ // Failed, remove the entry and return null
entry.destroy();
entryMap.remove(key);
return null;
}
}
Object value = entry.getValue();
return value;
}
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. |
public int getDefaultLifetime() {
return defaultLifetime;
}
Get the default lifetime of cache entries. |
public int getResolution() {
return resolution;
}
Get the frequency of the current time snapshot. |
public List getValidKeys() {
ArrayList validKeys = new ArrayList();
synchronized( entryMap )
{
Iterator iter = entryMap.entrySet().iterator();
while( iter.hasNext() )
{
Map.Entry entry = (Map.Entry) iter.next();
TimedEntry value = (TimedEntry) entry.getValue();
if( value.isCurrent(now) == true )
validKeys.add(entry.getKey());
}
}
return validKeys;
}
Get the list of keys for entries that are not expired. |
public void insert(Object key,
Object value) {
if( entryMap.containsKey(key) )
throw new IllegalStateException("Attempt to insert duplicate entry");
TimedEntry entry = null;
if( (value instanceof TimedEntry) == false )
{ // Wrap the value in a DefaultTimedEntry
entry = new DefaultTimedEntry(defaultLifetime, value);
}
else
{
entry = (TimedEntry) value;
}
entry.init(now);
entryMap.put(key, entry);
}
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. |
public Object peek(Object key) {
TimedEntry entry = (TimedEntry) entryMap.get(key);
Object value = null;
if( entry != null )
value = entry.getValue();
return value;
}
Get the cache value for key. This method does not check to see if
the entry has expired. |
public TimedEntry peekEntry(Object key) {
TimedEntry entry = (TimedEntry) entryMap.get(key);
return entry;
}
Get the raw TimedEntry for key without performing any expiration check. |
public void remove(Object key) {
TimedEntry entry = (TimedEntry) entryMap.remove(key);
if( entry != null )
entry.destroy();
}
Remove the entry associated with key and call destroy on the entry
if found. |
public void run() {
now = System.currentTimeMillis();
}
The TimerTask run method. It updates the cache time to the
current system time. |
public synchronized void setDefaultLifetime(int defaultLifetime) {
this.defaultLifetime = defaultLifetime;
}
Set the default lifetime of cache entries for new values added to the cache. |
public synchronized void setResolution(int resolution) {
if( resolution < = 0 )
resolution = 60;
if( resolution != this.resolution )
{
this.resolution = resolution;
theTimer.cancel();
theTimer = new ResolutionTimer();
resolutionTimer.scheduleAtFixedRate(theTimer, 0, 1000*resolution);
}
}
Set the cache timer resolution |
public int size() {
return entryMap.size();
}
|
public void start() {
theTimer = new ResolutionTimer();
resolutionTimer.scheduleAtFixedRate(theTimer, 0, 1000*resolution);
}
Schedules this with the class resolutionTimer Timer object for
execution every resolution seconds. |
public void stop() {
theTimer.cancel();
flush();
}
Stop cancels the resolution timer and flush()es the cache. |