org.egothor.cache.slru
Class SlruCache<K,V>

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

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

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


Field Summary
 
Fields inherited from class org.egothor.cache.AbstractCache
capacity, DEFAULT_CAPACITY, hits, misses, resolver
 
Constructor Summary
SlruCache()
          Constructor for the SlruCache object.
SlruCache(int capacity)
          Constructor for the SlruCache object.
SlruCache(int capacity, int protectedCapacity)
          Constructor for the SlruCache object.
SlruCache(Resolver<K,V> resolver)
          Constructor for the SlruCache object.
SlruCache(Resolver<K,V> resolver, int capacity)
          Constructor for the SlruCache object.
SlruCache(Resolver<K,V> resolver, int capacity, int protectedCapacity)
          Constructor for the SlruCache object.
 
Method Summary
 void clear()
          Removes all key->value mappings from the cache and cleans both protected and probationary queues.
 boolean containsKey(K key)
          Checks whether the specified key is cached.
 void evict()
          Removes the LRU key from the probationary or protected segment, if probationary is empty.
 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 key from the probationary or protected segment, if probationary is empty.
 int protectedCapacity()
          Returns the capacity of the protected segment of the cache.
 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

SlruCache

public SlruCache()
Constructor for the SlruCache object. Capacity of the cache and the protected segment is set to default and the resolver always returns null.


SlruCache

public SlruCache(int capacity)
Constructor for the SlruCache object. Capacity of the protected segment is set to default and resolver always return null.

Parameters:
capacity - capacity of the cache

SlruCache

public SlruCache(int capacity,
                 int protectedCapacity)
Constructor for the SlruCache object. Resolver always returns null.

Parameters:
capacity - capacity of the cache
protectedCapacity - capacity of the protected segment of the cache

SlruCache

public SlruCache(Resolver<K,V> resolver)
Constructor for the SlruCache object. Capacity of the cache and the protected segment is set to default.

Parameters:
resolver - resolver to associate with the cache

SlruCache

public SlruCache(Resolver<K,V> resolver,
                 int capacity)
Constructor for the SlruCache object. Capacity of the protected segment is set to default.

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

SlruCache

public SlruCache(Resolver<K,V> resolver,
                 int capacity,
                 int protectedCapacity)
Constructor for the SlruCache object.

Parameters:
resolver - resolver to associate with the cache
capacity - capacity of the cache
protectedCapacity - capacity of the protected segment of the cache
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 SLRU 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 both protected and probationary 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 key from the probationary or protected segment, if probationary is empty.

Specified by:
nextEvicted in interface Cache<K,V>
Specified by:
nextEvicted in class AbstractCache<K,V>
Returns:
LRU key from the probationary or protected segment

evict

public void evict()
Removes the LRU key from the probationary or protected segment, if probationary is empty.

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

protectedCapacity

public int protectedCapacity()
Returns the capacity of the protected segment of the cache.

Returns:
capacity of the protected segment

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