com.webmethods.caf.wsclient.cache.impl
Class WSLruCache

java.lang.Object
  extended by com.webmethods.caf.wsclient.cache.impl.WSLruCache

public class WSLruCache
extends Object

Fixed length cache with a LRU replacement policy. If cache items implement CacheListener, they will be informed when they're removed from the cache.

Null keys are not allowed. WSLruCache is synchronized.


Field Summary
protected  int m_cacheCapacity
           
protected  Map m_cacheMap
           
protected  long m_entryTimeout
           
 
Constructor Summary
WSLruCache(int maxCapacity, long maxFreshTimeout)
          Create the LRU cache with a specific capacity and freshness timeout.
 
Method Summary
 void clear()
          Clears all entries from the cache
 boolean containsKey(Object key)
          Determines if an item is in the Cache
 Object get(Object key)
          Get an item from the cache and make it most recently used.
protected  Object getFresh(Object key)
          Get an item from the hash and return it.
 Iterator getFreshKeys()
          Returns the fresh keys stored in the cache Note: this is a snapshot of the keys and is therefore safe to iterate, however care needs to be taken when using these keys for cache lookups as the key may no longer exist.
 Iterator getFreshValues()
          Returns the values stored in the cache Note: this is a snapshot of the values and is therefore safe to iterate but may not reflect the current state of the cache.
 Iterator getKeys()
          Get an iterator for the keys of the cache, including both fresh and stale entries Note: this is a snapshot of the keys and is therefore safe to iterate, however care needs to be taken when using these keys for cache lookups as the key may no longer exist.
 int getMaxCapacity()
          The current capacity of this cache
 Iterator getValues()
          Get an iterator for the values of the cache that includes both stale and fresh enties Note: this is a snapshot of the values and is therefore safe to iterate but may not reflect the current state of the cache.
protected  void printCacheToConsole()
           
 Object put(Object key, Object value)
          Puts a new item in the cache and returns the previous value stored under that key
 Object remove(Object key)
          Removes an item from the cache
 int size()
          The current size of the cache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_cacheMap

protected Map m_cacheMap

m_cacheCapacity

protected int m_cacheCapacity

m_entryTimeout

protected long m_entryTimeout
Constructor Detail

WSLruCache

public WSLruCache(int maxCapacity,
                  long maxFreshTimeout)
Create the LRU cache with a specific capacity and freshness timeout. Entries older than a specific staleness will act as a cache miss.

Parameters:
maxCapacity - - minimum capacity of the cache
maxCapacity - - maximum cache entry age (in milliseconds)
Method Detail

clear

public void clear()
Clears all entries from the cache


size

public int size()
The current size of the cache

Returns:
the current queue size as int

getMaxCapacity

public int getMaxCapacity()
The current capacity of this cache

Returns:
the queue capacity

containsKey

public boolean containsKey(Object key)
Determines if an item is in the Cache

Parameters:
key - the unique key to the item in the cache
Returns:
true if the cache contains an entry for the given key

get

public Object get(Object key)
Get an item from the cache and make it most recently used.

Parameters:
key - key to lookup the item
Returns:
the matching object in the cache

getFresh

protected Object getFresh(Object key)
Get an item from the hash and return it. If the item is stale, it is removed from the hash and null is returned.

Parameters:
key - - the cache key object
Returns:
the fresh cache entry if found, otherwise null

put

public Object put(Object key,
                  Object value)
Puts a new item in the cache and returns the previous value stored under that key

Parameters:
key - key to store data
value - value to be stored
Returns:
old value stored under the key

remove

public Object remove(Object key)
Removes an item from the cache

Parameters:
key - the key to remove
Returns:
the value removed

getKeys

public Iterator getKeys()
Get an iterator for the keys of the cache, including both fresh and stale entries Note: this is a snapshot of the keys and is therefore safe to iterate, however care needs to be taken when using these keys for cache lookups as the key may no longer exist.

Returns:
an Iterator containing the cache keys as Objects

getFreshKeys

public Iterator getFreshKeys()
Returns the fresh keys stored in the cache Note: this is a snapshot of the keys and is therefore safe to iterate, however care needs to be taken when using these keys for cache lookups as the key may no longer exist.

Returns:
an Iterator containing those cache keys that have not expired as Objects

getValues

public Iterator getValues()
Get an iterator for the values of the cache that includes both stale and fresh enties Note: this is a snapshot of the values and is therefore safe to iterate but may not reflect the current state of the cache.

Returns:
an Iterator of cache value as CacheItem objects

getFreshValues

public Iterator getFreshValues()
Returns the values stored in the cache Note: this is a snapshot of the values and is therefore safe to iterate but may not reflect the current state of the cache.

Returns:
an Iterator of cache values that have not expired as CacheItem objects

printCacheToConsole

protected void printCacheToConsole()