tuwien.auto.calimero.buffer.cache
Class LFUCache

java.lang.Object
  extended by tuwien.auto.calimero.buffer.cache.ExpiringCache
      extended by tuwien.auto.calimero.buffer.cache.LFUCache
All Implemented Interfaces:
Cache

public class LFUCache
extends ExpiringCache

A Cache using a LFU replacement policy.

The usage value of CacheObject.getUsage() equals the access count, CacheObject.getCount().


Nested Class Summary
 
Nested classes/interfaces inherited from interface tuwien.auto.calimero.buffer.cache.Cache
Cache.Statistic
 
Field Summary
 
Fields inherited from class tuwien.auto.calimero.buffer.cache.ExpiringCache
defaultSweepInterval, map, sweepInterval
 
Constructor Summary
LFUCache(int cacheSize, int timeToExpire)
          Creates a new LFU cache.
 
Method Summary
 void clear()
          Empties the cache of all CacheObjects.
 CacheObject get(java.lang.Object key)
          Gets the CacheObject associated with key from the cache.
protected  void notifyRemoved(CacheObject obj)
          Override this method to get notified when ExpiringCache.removeExpired() removed a CacheObject from the ExpiringCache.map.
 void put(CacheObject obj)
          Inserts the CacheObject obj into the cache.
 void remove(java.lang.Object key)
          Removes the CacheObject associated with key from the cache, if found.
 Cache.Statistic statistic()
          Returns information collected by this cache since its creation.
 
Methods inherited from class tuwien.auto.calimero.buffer.cache.ExpiringCache
removeExpired, startSweeper, stopSweeper
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LFUCache

public LFUCache(int cacheSize,
                int timeToExpire)
Creates a new LFU cache.

Optionally, a maximum cache size and an expiring time can be specified.

Parameters:
cacheSize - maximum number of CacheObjects in the cache, or 0 for no maximum
timeToExpire - time in seconds for cache objects to stay valid, or 0 for no expiring
Method Detail

clear

public void clear()
Description copied from interface: Cache
Empties the cache of all CacheObjects.


get

public CacheObject get(java.lang.Object key)
Description copied from interface: Cache
Gets the CacheObject associated with key from the cache.

If found, the access count of the CacheObject is incremented by 1.

Parameters:
key - key to search
Returns:
the CacheObject or null if key does not exist in the cache

notifyRemoved

protected final void notifyRemoved(CacheObject obj)
Description copied from class: ExpiringCache
Override this method to get notified when ExpiringCache.removeExpired() removed a CacheObject from the ExpiringCache.map.

Overrides:
notifyRemoved in class ExpiringCache
Parameters:
obj - removed CacheObject

put

public void put(CacheObject obj)
Inserts the CacheObject obj into the cache.

If a CacheObject with an equal key (delivered by CacheObject.getKey()) to obj.getKey() is already in the cache, it will be replaced by obj.
CacheObject.resetTimestamp() is invoked on obj after obj was inserted successfully.
If expiring of cache objects is set, and the timestamp of a CacheObject is renewed after it has been put into the cache, a new put(CacheObject) is required for that object to apply the timestamp and keep the cache in a consistent state.

Parameters:
obj - CacheObject to put into the cache

remove

public void remove(java.lang.Object key)
Description copied from interface: Cache
Removes the CacheObject associated with key from the cache, if found.

Parameters:
key - key of CacheObject to remove

statistic

public Cache.Statistic statistic()
Description copied from interface: Cache
Returns information collected by this cache since its creation.

Returns:
a Cache.Statistic object