com.caucho.util
Class LruCache<K,V>

java.lang.Object
  extended by com.caucho.util.LruCache<K,V>

public final class LruCache<K,V>
extends java.lang.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. LruCache is synchronized.


Nested Class Summary
static interface LruCache.Entry<K,V>
          Interface for entry iterator;
 
Constructor Summary
LruCache(int initialCapacity)
          Create the LRU cache with a specific capacity.
LruCache(int initialCapacity, boolean isStatistics)
          Create the LRU cache with a specific capacity.
 
Method Summary
 void clear()
          Clears the cache
 boolean compareAndPut(V testValue, K key, V value)
          Puts a new item in the cache if the current value matches oldValue.
 V get(K key)
          Get an item from the cache and make it most recently used.
 int getCapacity()
          Returns the LRU cache capacity
 long getHitCount()
          Returns the hit count.
 long getMissCount()
          Returns the miss count.
 java.util.Iterator<LruCache.Entry<K,V>> iterator()
          Returns the entries
 java.util.Iterator<K> keys()
          Returns the keys stored in the cache
 java.util.Iterator<K> keys(java.util.Iterator<K> oldIter)
          Returns keys stored in the cache using an old iterator
 V put(K key, V value)
          Puts a new item in the cache.
 V putIfNew(K key, V value)
          Puts a new item in the cache.
 V remove(K key)
          Removes an item from the cache
 boolean removeLongestTail()
          Remove the last item in the LRU.
 boolean removeTail()
          Remove the last item in the LRU
 void setEnableListeners(boolean isEnable)
          Disable the listeners
 void setEnableStatistics(boolean isEnable)
           
 int size()
          Returns the current number of entries in the cache.
 java.util.Iterator<V> values()
          Returns the values in the cache
 java.util.Iterator<V> values(java.util.Iterator<V> oldIter)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LruCache

public LruCache(int initialCapacity)
Create the LRU cache with a specific capacity.

Parameters:
initialCapacity - minimum capacity of the cache

LruCache

public LruCache(int initialCapacity,
                boolean isStatistics)
Create the LRU cache with a specific capacity.

Parameters:
initialCapacity - minimum capacity of the cache
Method Detail

setEnableListeners

public void setEnableListeners(boolean isEnable)
Disable the listeners


setEnableStatistics

public void setEnableStatistics(boolean isEnable)

size

public int size()
Returns the current number of entries in the cache.


getCapacity

public int getCapacity()
Returns the LRU cache capacity


clear

public void clear()
Clears the cache


get

public V get(K 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

put

public V put(K key,
             V value)
Puts a new item in the cache. If the cache is full, remove the LRU item.

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

putIfNew

public V putIfNew(K key,
                  V value)
Puts a new item in the cache. If the cache is full, remove the LRU item.

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

compareAndPut

public boolean compareAndPut(V testValue,
                             K key,
                             V value)
Puts a new item in the cache if the current value matches oldValue.

Parameters:
key - the key
value - the new value
testValue - the value to test against the current
Returns:
true if the put succeeds

removeTail

public boolean removeTail()
Remove the last item in the LRU


removeLongestTail

public boolean removeLongestTail()
Remove the last item in the LRU. In this case, remove from the list with the longest length. For functions like Cache disk space, this is a better solution than the struct LRU removal.


remove

public V remove(K key)
Removes an item from the cache

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

keys

public java.util.Iterator<K> keys()
Returns the keys stored in the cache


keys

public java.util.Iterator<K> keys(java.util.Iterator<K> oldIter)
Returns keys stored in the cache using an old iterator


values

public java.util.Iterator<V> values()
Returns the values in the cache


values

public java.util.Iterator<V> values(java.util.Iterator<V> oldIter)

iterator

public java.util.Iterator<LruCache.Entry<K,V>> iterator()
Returns the entries


getHitCount

public long getHitCount()
Returns the hit count.


getMissCount

public long getMissCount()
Returns the miss count.