org.egothor.cache.lruk
Class LruKCache<K,V>

java.lang.Object
  extended by org.egothor.cache.AbstractCache<K,V>
      extended by org.egothor.cache.lruk.LruKCache<K,V>
Type Parameters:
K - type of the key
V - type of the value
All Implemented Interfaces:
Cache<K,V>

public class LruKCache<K,V>
extends AbstractCache<K,V>

Implementation of Cache that uses the LRU-K algorithm as an eviction policy.


Field Summary
protected static int DEFAULT_NUMBER_OF_IDS
          Default number of ID's the LRU-K algorithm should use.
 
Fields inherited from class org.egothor.cache.AbstractCache
capacity, DEFAULT_CAPACITY, hits, misses, resolver
 
Constructor Summary
LruKCache()
          Constructor for the LruKCache object.
LruKCache(int capacity, int historyCapacity, int numberOfIds)
          Constructor for the LruKCache object.
LruKCache(Resolver<K,V> resolver)
          Constructor for the LruKCache object.
LruKCache(Resolver<K,V> resolver, int capacity)
          Constructor for the LruKCache object.
LruKCache(Resolver<K,V> resolver, int capacity, int historyCapacity, int numberOfIds)
          Constructor for the LruKCache object.
 
Method Summary
 void clear()
          Removes all key->value mappings from the cache and cleans the history queue.
 boolean containsKey(K key)
          Checks whether the specified key is cached.
 void evict()
          Removes the key with the highest backward-K-distance.
 V get(K key)
          Gets the value associated with the specified key from the cache if a mapping exists, or the value returned by the resolver.
 int historyCapacity()
          Returns the maximum capacity of the history queue.
 java.util.Set<K> keySet()
          Gets all keys contained in the cache.
 K nextEvicted()
          Gets the key with the highest backward-K-distance.
 int numberOfIds()
          Returns the number of id's the LRU-K algorithm uses.
 V put(K key, V value)
          Adds a new key->value mapping to the cache.
 V remove(K key)
          Removes the specified key and its value from the cache.
 int size()
          Returns the number of key->value mappings in the cache.
 java.lang.String toString()
          Returns a text representation of the cache.
 V update(K key, V value)
          Updates the value associated with the specified key in the cache if a mapping exists for the key.
 
Methods inherited from class org.egothor.cache.AbstractCache
capacity, getResolver, hitRatio, hits, misses, requests, resetCounters, setResolver
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_NUMBER_OF_IDS

protected static final int DEFAULT_NUMBER_OF_IDS
Default number of ID's the LRU-K algorithm should use.

See Also:
Constant Field Values
Constructor Detail

LruKCache

public LruKCache()
Constructor for the LruKCache object. Uses default capacity, history capacity, number of id's and the resolver always returns null.


LruKCache

public LruKCache(Resolver<K,V> resolver)
Constructor for the LruKCache object. Uses default capacity, history capacity and number of id's.

Parameters:
resolver - resolver to associate with the cache

LruKCache

public LruKCache(int capacity,
                 int historyCapacity,
                 int numberOfIds)
Constructor for the LruKCache object. Resolver always returns null.

Parameters:
capacity - capacity of the cache
historyCapacity - capacity of the history queue
numberOfIds - number of id's the LRU-K algorithm should use

LruKCache

public LruKCache(Resolver<K,V> resolver,
                 int capacity)
Constructor for the LruKCache object. Uses default history capacity and number of id's.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache

LruKCache

public LruKCache(Resolver<K,V> resolver,
                 int capacity,
                 int historyCapacity,
                 int numberOfIds)
          throws java.lang.IllegalArgumentException
Constructor for the LruKCache object.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
historyCapacity - capacity of the history queue
numberOfIds - number of id's the LRU-K algorithm should use
Throws:
java.lang.IllegalArgumentException - when history capacity is smaller or equal to zero
Method Detail

put

public V put(K key,
             V value)
Adds a new key->value mapping to the cache. If a mapping previously existed for the specified key, it is removed at first. If the cache is full, the key with the highest backward-K-distance is removed.

Specified by:
put in interface Cache<K,V>
Overrides:
put in class AbstractCache<K,V>
Parameters:
key - key to add to the cache
value - value to map the key on
Returns:
previous value associated with the key or null if a previous mapping did not exists in the cache for the specified key

get

public V get(K key)
Gets the value associated with the specified key from the cache if a mapping exists, or the value returned by the resolver.

Specified by:
get in interface Cache<K,V>
Overrides:
get in class AbstractCache<K,V>
Parameters:
key - key to get the value for
Returns:
value associated with the key from the cache, or from the resolver if a mapping does not exits in the cache

remove

public V remove(K key)
Removes the specified key and its value from the cache.

Specified by:
remove in interface Cache<K,V>
Overrides:
remove in class AbstractCache<K,V>
Parameters:
key - key to remove from the cache.
Returns:
value associated with the key, or null if a mapping did not exist

update

public V update(K key,
                V value)
Updates the value associated with the specified key in the cache if a mapping exists for the key.

Specified by:
update in interface Cache<K,V>
Overrides:
update in class AbstractCache<K,V>
Parameters:
key - key to change the value for
value - new value associated with the key
Returns:
previous associated value or null if the key is not cached

containsKey

public boolean containsKey(K key)
Checks whether the specified key is cached.

Specified by:
containsKey in interface Cache<K,V>
Specified by:
containsKey in class AbstractCache<K,V>
Parameters:
key - key to check
Returns:
true, if the key is currently cached, false otherwise

clear

public void clear()
Removes all key->value mappings from the cache and cleans the history queue.


keySet

public java.util.Set<K> keySet()
Gets all keys contained in the cache.

Returns:
Set of keys contained in the cache.

size

public int size()
Returns the number of key->value mappings in the cache.

Specified by:
size in interface Cache<K,V>
Specified by:
size in class AbstractCache<K,V>
Returns:
number of key->value mappings in the cache

nextEvicted

public K nextEvicted()
Gets the key with the highest backward-K-distance.

Specified by:
nextEvicted in interface Cache<K,V>
Specified by:
nextEvicted in class AbstractCache<K,V>
Returns:
key, that has the highest backward-K-distance

evict

public void evict()
Removes the key with the highest backward-K-distance.

Specified by:
evict in interface Cache<K,V>
Specified by:
evict in class AbstractCache<K,V>

numberOfIds

public int numberOfIds()
Returns the number of id's the LRU-K algorithm uses.

Returns:
number of id's LRU-K uses

historyCapacity

public int historyCapacity()
Returns the maximum capacity of the history queue.

Returns:
capacity of the history queue

toString

public java.lang.String toString()
Returns a text representation of the cache.

Specified by:
toString in class AbstractCache<K,V>
Returns:
text representation of the cache