org.egothor.cache.multiQueue
Class MultiQueueCache<K,V>

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

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

Implementation of Cache that uses the MQ algorithm as an eviction policy.


Field Summary
static byte DEFAULT_NUMBER_OF_QUEUES
          Default number of queues the MQ algorithm should use.
 
Fields inherited from class org.egothor.cache.AbstractCache
capacity, DEFAULT_CAPACITY, hits, misses, resolver
 
Constructor Summary
MultiQueueCache()
          Constructor for the MultiQueueCache object.
MultiQueueCache(int capacity)
          Constructor for the MultiQueueCache object.
MultiQueueCache(int capacity, int outCapacity)
          Constructor for the MultiQueueCache object.
MultiQueueCache(int capacity, int outCapacity, byte numberOfQueues)
          Constructor for the MultiQueueCache object.
MultiQueueCache(int capacity, int outCapacity, byte numberOfQueues, MultiQueueFunction queueFunction)
          Constructor for the MultiQueueCache object.
MultiQueueCache(Resolver<K,V> resolver)
          Constructor for the MultiQueueCache object.
MultiQueueCache(Resolver<K,V> resolver, int capacity)
          Constructor for the MultiQueueCache object.
MultiQueueCache(Resolver<K,V> resolver, int capacity, int outCapacity)
          Constructor for the MultiQueueCache object.
MultiQueueCache(Resolver<K,V> resolver, int capacity, int outCapacity, byte numberOfQueues)
          Constructor for the MultiQueueCache object.
MultiQueueCache(Resolver<K,V> resolver, int capacity, int outCapacity, byte numberOfQueues, MultiQueueFunction queueFunction)
          Constructor for the MultiQueueCache object.
 
Method Summary
 void clear()
          Removes all key->value mappings from the cache and cleans all queues.
 boolean containsKey(K key)
          Checks whether the specified key is cached.
 void evict()
          Removes the LRU item of the first LRU queue from the cache.
 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.
 java.util.Set<K> keySet()
          Gets all keys contained in the cache.
 K nextEvicted()
          Gets the LRU item of the first non-empty LRU queue.
 int numberOfQueues()
          Returns the number of queues used by the MQ algorithm.
 int outCapacity()
          Returns the capacity of the history QOut queue.
 V put(K key, V value)
          Adds a new key->value mapping to the cache.
 MultiQueueFunction queueFunction()
          Gets the class that is used to compute the queue number.
 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_QUEUES

public static byte DEFAULT_NUMBER_OF_QUEUES
Default number of queues the MQ algorithm should use.

Constructor Detail

MultiQueueCache

public MultiQueueCache()
Constructor for the MultiQueueCache object. Uses default cache capacity, default QOut capacity, default number of queues, log2 as a queue function and the resolver always returns null.


MultiQueueCache

public MultiQueueCache(int capacity)
Constructor for the MultiQueueCache object. Uses default QOut capacity, default number of queues, log2 as a queue function and the resolver always returns null.

Parameters:
capacity - capacity of the cache

MultiQueueCache

public MultiQueueCache(int capacity,
                       int outCapacity)
Constructor for the MultiQueueCache object. Uses default number of queues, log2 as a queue function and the resolver always returns null.

Parameters:
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue

MultiQueueCache

public MultiQueueCache(int capacity,
                       int outCapacity,
                       byte numberOfQueues)
Constructor for the MultiQueueCache object. Uses log2 as a queue function and the resolver always returns null.

Parameters:
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue
numberOfQueues - number of queues that should be used by the MQ algorithm

MultiQueueCache

public MultiQueueCache(int capacity,
                       int outCapacity,
                       byte numberOfQueues,
                       MultiQueueFunction queueFunction)
Constructor for the MultiQueueCache object. Resolver always returns null.

Parameters:
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue
numberOfQueues - number of queues that should be used by the MQ algorithm
queueFunction - queue function that should be used by the MQ algorithm

MultiQueueCache

public MultiQueueCache(Resolver<K,V> resolver)
Constructor for the MultiQueueCache object. Uses default cache capacity, default QOut capacity, default number of queues and log2 as a queue function.

Parameters:
resolver - resolver to associate with the cache

MultiQueueCache

public MultiQueueCache(Resolver<K,V> resolver,
                       int capacity)
Constructor for the MultiQueueCache object. Uses default QOut capacity, default number of queues and log2 as a queue function.

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

MultiQueueCache

public MultiQueueCache(Resolver<K,V> resolver,
                       int capacity,
                       int outCapacity)
Constructor for the MultiQueueCache object. Uses default number of queues and log2 as a queue function.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue

MultiQueueCache

public MultiQueueCache(Resolver<K,V> resolver,
                       int capacity,
                       int outCapacity,
                       byte numberOfQueues)
Constructor for the MultiQueueCache object. Uses log2 as a queue function.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue
numberOfQueues - number of queues that should be used by the MQ algorithm

MultiQueueCache

public MultiQueueCache(Resolver<K,V> resolver,
                       int capacity,
                       int outCapacity,
                       byte numberOfQueues,
                       MultiQueueFunction queueFunction)
Constructor for the MultiQueueCache object.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
outCapacity - capacity of the history QOut queue
numberOfQueues - number of queues that should be used by the MQ algorithm
queueFunction - queue function that should be used by the MQ algorithm
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 LRU item of the first non-empty queue 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 all queues.


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 LRU item of the first non-empty LRU queue.

Specified by:
nextEvicted in interface Cache<K,V>
Specified by:
nextEvicted in class AbstractCache<K,V>
Returns:
LRU item of the first non-empty queue

evict

public void evict()
Removes the LRU item of the first LRU queue from the cache.

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

outCapacity

public int outCapacity()
Returns the capacity of the history QOut queue.

Returns:
capacity of QOut queue

numberOfQueues

public int numberOfQueues()
Returns the number of queues used by the MQ algorithm.

Returns:
number of queues used by the MQ algorithm

queueFunction

public MultiQueueFunction queueFunction()
Gets the class that is used to compute the queue number.

Returns:
queue function used by the cache

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