Class LRUCache<K,V>

java.lang.Object
org.jboss.jca.adapters.jdbc.util.LRUCache<K,V>
Type Parameters:
K - The key
V - The value
K - The key
V - The value
All Implemented Interfaces:
Cache<K,V>

public class LRUCache<K,V> extends Object implements Cache<K,V>
Implementation of a Least Recently Used cache policy.
Author:
Simone Bordet
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Double linked cell used as entry in the cache list.
    class 
    Double queued list used to store cache entries.
  • Constructor Summary

    Constructors
    Constructor
    Description
    LRUCache(int max)
    Creates a LRU cache
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Flushes the cached objects from the cache.
    get(K key)
    Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
    void
    insert(K key, V o)
    Inserts the specified object into the cache following the implemented policy.
    peek(K key)
    Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
    void
    remove(K key)
    Remove the cached object paired with the specified key.
    void
    Set the cache listener
    int
    The cache size

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LRUCache

      public LRUCache(int max)
      Creates a LRU cache
      Parameters:
      max - The maximum number of entries
  • Method Details

    • get

      public V get(K key)
      Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
      Implementations of this method must have complexity of order O(1). Differently from Cache.peek(K) this method not only return whether the object is present in the cache or not, but also applies the implemented policy that will "refresh" the cached object in the cache, because this cached object was really requested.
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - the key paired with the object
      Returns:
      the object
      See Also:
    • peek

      public V peek(K key)
      Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
      Implementations of this method must have complexity of order O(1). This method should not apply the implemented caching policy to the object paired with the given key, so that a client can query if an object is cached without "refresh" its cache status. Real requests for the object must be done using Cache.get(K).
      Specified by:
      peek in interface Cache<K,V>
      Parameters:
      key - the key paired with the object
      Returns:
      the object
      See Also:
    • insert

      public void insert(K key, V o)
      Inserts the specified object into the cache following the implemented policy.
      Implementations of this method must have complexity of order O(1).
      Specified by:
      insert in interface Cache<K,V>
      Parameters:
      key - the key paired with the object
      o - the object to cache
      See Also:
    • remove

      public void remove(K key)
      Remove the cached object paired with the specified key.
      Implementations of this method must have complexity of order O(1).
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - the key paired with the object
      See Also:
    • flush

      public void flush()
      Flushes the cached objects from the cache.
      Specified by:
      flush in interface Cache<K,V>
    • size

      public int size()
      The cache size
      Specified by:
      size in interface Cache<K,V>
      Returns:
      the size of the cache
    • setListener

      public void setListener(CacheListener listener)
      Set the cache listener
      Specified by:
      setListener in interface Cache<K,V>
      Parameters:
      listener - The listener