org.egothor.cache.twoQueue
Class TwoQueueCache<K,V>

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

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

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


Field Summary
 
Fields inherited from class org.egothor.cache.AbstractCache
capacity, DEFAULT_CAPACITY, hits, misses, resolver
 
Constructor Summary
TwoQueueCache()
          Constructor for the TwoQueueCache object.
TwoQueueCache(int capacity)
          Constructor for the TwoQueueCache object.
TwoQueueCache(int capacity, int kIn, int kOut)
          Constructor for the TwoQueueCache object.
TwoQueueCache(Resolver<K,V> resolver)
          Constructor for the TwoQueueCache object.
TwoQueueCache(Resolver<K,V> resolver, int capacity)
          Constructor for the TwoQueueCache object.
TwoQueueCache(Resolver<K,V> resolver, int capacity, int kIn, int kOut)
          Constructor for the TwoQueueCache 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 next key from the cache according to the 2Q algorithm.
 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 inCapacity()
          Returns the capacity of the history AIn FIFO queue.
 java.util.Set<K> keySet()
          Gets all keys contained in the cache.
 K nextEvicted()
          Gets the next key that would be removed from the cache according to the 2Q algorithm.
 int outCapacity()
          Returns the capacity of the history AOut LRU queue.
 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
 

Constructor Detail

TwoQueueCache

public TwoQueueCache()
Constructor for the TwoQueueCache object. Uses default cache, AIn and AOut capacity and the resolver alwasy return null.


TwoQueueCache

public TwoQueueCache(int capacity)
Constructor for the TwoQueueCache object. Uses default AIn and AOut capacity abd the resolver always returns null.

Parameters:
capacity - capacity of the cache

TwoQueueCache

public TwoQueueCache(int capacity,
                     int kIn,
                     int kOut)
Constructor for the TwoQueueCache object. Resolver always return null.

Parameters:
capacity - capacity of the cache
kIn - capacity of AIn queue
kOut - capacity of AOut queue

TwoQueueCache

public TwoQueueCache(Resolver<K,V> resolver)
Constructor for the TwoQueueCache object. Uses default cache, AIn and AOut capacity.

Parameters:
resolver - resolver to associate with the cache

TwoQueueCache

public TwoQueueCache(Resolver<K,V> resolver,
                     int capacity)
Constructor for the TwoQueueCache object. Uses default AIn and AOut capacity.

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

TwoQueueCache

public TwoQueueCache(Resolver<K,V> resolver,
                     int capacity,
                     int kIn,
                     int kOut)
Constructor for the TwoQueueCache object.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
kIn - capacity of AIn queue
kOut - capacity of AOut queue
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, a mapping is removed from the cache according to 2Q algorithm.

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 next key that would be removed from the cache according to the 2Q algorithm.

Specified by:
nextEvicted in interface Cache<K,V>
Specified by:
nextEvicted in class AbstractCache<K,V>
Returns:
key, that would be removed next from the cache or null if the cache is empty

evict

public void evict()
Removes the next key from the cache according to the 2Q algorithm.

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

inCapacity

public int inCapacity()
Returns the capacity of the history AIn FIFO queue.

Returns:
capacity of AIn queue

outCapacity

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

Returns:
capacity of AOut 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