tuwien.auto.calimero.buffer.cache
Class ExpiringCache

java.lang.Object
  extended by tuwien.auto.calimero.buffer.cache.ExpiringCache
All Implemented Interfaces:
Cache
Direct Known Subclasses:
LFUCache, PositiveListCache

public abstract class ExpiringCache
extends java.lang.Object
implements Cache

Implements a cache expiring mechanism for CacheObjects.

A time span is specified for how long a value is considered valid. After that time for expiring, the cache object is removed on the next call of removeExpired() by an internal CacheSweeper. If a cache with expiring cache objects is not used anymore, invoke Cache.clear() to quit the running cache sweeping mechanism.
The timestamp of CacheObject.getTimestamp() is used to determine if a cache object value has expired.
Note that if the timestamp of a CacheObject changes after it was put into the cache, the object has to be reinserted (see Cache.put(CacheObject)) to keep the cache in a consistent state. If no expiring is used, this might be omitted.


Nested Class Summary
 
Nested classes/interfaces inherited from interface tuwien.auto.calimero.buffer.cache.Cache
Cache.Statistic
 
Field Summary
protected static int defaultSweepInterval
          Default sweep interval in seconds used for a CacheSweeper (60 seconds).
protected  java.util.Map map
          The map holding the CacheObjects.
protected  int sweepInterval
          Sweep interval in seconds used for the CacheSweeper.
 
Constructor Summary
ExpiringCache(int timeToExpire)
          Creates an ExpiringCache.
 
Method Summary
protected  void notifyRemoved(CacheObject obj)
          Override this method to get notified when removeExpired() removed a CacheObject from the map.
 void removeExpired()
          Removes all CacheObjects, where
CacheObject.getTimestamp() + timeToExpire <= now, with timeToExpire > 0 and now is the point of time removeExpired() is invoked.
protected  void startSweeper()
          Starts a new CacheSweeper, if not already running, and if an expiring time for CacheObject was specified.
protected  void stopSweeper()
          Stops the CacheSweeper, if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface tuwien.auto.calimero.buffer.cache.Cache
clear, get, put, remove, statistic
 

Field Detail

defaultSweepInterval

protected static final int defaultSweepInterval
Default sweep interval in seconds used for a CacheSweeper (60 seconds).

See Also:
Constant Field Values

map

protected java.util.Map map
The map holding the CacheObjects.

The map instance itself is not synchronized, synchronization is done using the cache object (this).


sweepInterval

protected int sweepInterval
Sweep interval in seconds used for the CacheSweeper.

It defaults to 60 seconds. For a new value to take effect, it has to be assigned either before the first startSweeper() call, or the cache sweeper has to be restarted.

Constructor Detail

ExpiringCache

public ExpiringCache(int timeToExpire)
Creates an ExpiringCache.

Note that for the actual begin of cache sweeping startSweeper() has to be invoked.

Parameters:
timeToExpire - time > 0 in seconds for cache entries to stay valid, on time = 0 no expiring of cache entries will occur
Method Detail

notifyRemoved

protected void notifyRemoved(CacheObject obj)
Override this method to get notified when removeExpired() removed a CacheObject from the map.

Parameters:
obj - removed CacheObject

removeExpired

public void removeExpired()
Removes all CacheObjects, where
CacheObject.getTimestamp() + timeToExpire <= now, with timeToExpire > 0 and now is the point of time removeExpired() is invoked.
If no expiring time was specified at creation of cache, no cache object will be expired.

Specified by:
removeExpired in interface Cache

startSweeper

protected final void startSweeper()
Starts a new CacheSweeper, if not already running, and if an expiring time for CacheObject was specified.
If the methods startSweeper and stopSweeper are invoked by different threads, they need to be synchronized.


stopSweeper

protected final void stopSweeper()
Stops the CacheSweeper, if any.