Class WSLruCache

java.lang.Object
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

    Fields
    Modifier and Type
    Field
    Description
    protected int
     
    protected Map<Object,com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem>
     
    protected long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    WSLruCache(int maxCapacity, long maxFreshTimeout)
    Create the LRU cache with a specific capacity and freshness timeout.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all entries from the cache
    boolean
    Determines if an item is in the Cache
    get(Object key)
    Get an item from the cache and make it most recently used.
    protected Object
    Get an item from the hash and return it.
    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<com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem>
    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.
    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
    The current capacity of this cache
    Iterator<com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem>
    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
     
    put(Object key, Object value)
    Puts a new item in the cache and returns the previous value stored under that key
    Removes an item from the cache
    int
    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 Details

    • m_cacheMap

      protected Map<Object,com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem> m_cacheMap
    • m_cacheCapacity

      protected int m_cacheCapacity
    • m_entryTimeout

      protected long m_entryTimeout
  • Constructor Details

    • 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 Details

    • 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<Object> 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<Object> 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<com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem> 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<com.webmethods.caf.wsclient.cache.impl.WSLruCache.CacheItem> 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()