org.apache.shindig.common.cache
Class LruCache<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
              extended by org.apache.shindig.common.cache.LruCache<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, Cache<K,V>

public class LruCache<K,V>
extends LinkedHashMap<K,V>
implements Cache<K,V>

A basic LRU cache. Prefer using EhCache for most purposes to this class.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
LruCache(int capacity)
           
 
Method Summary
 void addElement(K key, V value)
          Stores an entry into the cache.
 long getCapacity()
          Returns the capacity of the cache.
 V getElement(K key)
          Retrieves an entry for the cache.
 long getSize()
           
protected  boolean removeEldestEntry(Map.Entry<K,V> eldest)
           
 V removeElement(K key)
          Removes an entry from the cache.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Constructor Detail

LruCache

public LruCache(int capacity)
Method Detail

getElement

public V getElement(K key)
Description copied from interface: Cache
Retrieves an entry for the cache.

Specified by:
getElement in interface Cache<K,V>
Returns:
The entry stored under the given key, or null if it doesn't exist.

addElement

public void addElement(K key,
                       V value)
Description copied from interface: Cache
Stores an entry into the cache.

Specified by:
addElement in interface Cache<K,V>

removeElement

public V removeElement(K key)
Description copied from interface: Cache
Removes an entry from the cache.

Specified by:
removeElement in interface Cache<K,V>
Parameters:
key - The entry to return.
Returns:
The entry stored under the given key, or null if it doesn't exist.

getCapacity

public long getCapacity()
Description copied from interface: Cache
Returns the capacity of the cache.

Specified by:
getCapacity in interface Cache<K,V>
Returns:
a positive integer indicating the upper bound on the number of allowed elements in the cace, -1 signifies that the capacity is unbounded

getSize

public long getSize()
Specified by:
getSize in interface Cache<K,V>
Returns:
The current size of the cache, or -1 if the cache does not support returning sizes.

removeEldestEntry

protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
Overrides:
removeEldestEntry in class LinkedHashMap<K,V>


Copyright © 2007-2012. All Rights Reserved.