org.egothor.cache.staticDynamic
Class StaticDynamicCache<K,V>

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

public class StaticDynamicCache<K,V>
extends java.lang.Object
implements Cache<K,V>

Implementation of Cache that uses a static and a dynamic cache.


Field Summary
static int DEFAULT_CAPACITY
          Default capacity of the static cache.
 
Constructor Summary
StaticDynamicCache(Cache<K,V> dynamicCache)
          Constructor for the StaticDynamicCache object.
StaticDynamicCache(Cache<K,V> dynamicCache, int staticCapacity)
          Constructor for the StaticDynamicCache object.
StaticDynamicCache(Resolver<K,V> resolver, Cache<K,V> dynamicCache)
          Constructor for the StaticDynamicCache object.
StaticDynamicCache(Resolver<K,V> resolver, Cache<K,V> dynamicCache, int staticCapacity)
          Constructor for the StaticDynamicCache object.
 
Method Summary
 int capacity()
          Returns the maximum number of key->value mappings cache can hold.
 void clear()
          Remove all key->value mappings from the static and the dynamic cache.
 boolean containsKey(K key)
          Checks whether the specified key is cached in static or dynamic cache.
 int dynamicCapacity()
          Returns the capacity of the dynamic cache.
 void evict()
          Removes a key from the dynamic cache according to its cache eviction policy.
 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.
 Resolver<K,V> getResolver()
          Gets the current resolver associated with this cache.
 float hitRatio()
          Returns the hits/requests ratio in percents.
 long hits()
          Returns the number of all hits.
 java.util.Set<K> keySet()
          Gets all keys contained in the cache.
 long misses()
          Returns the number of all misses.
 K nextEvicted()
          Gets the next key that would be removed from the dynamic cache according to the current cache replacement policy.
 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.
 long requests()
          Returns the number of all submitted requests
 void resetCounters()
          Sets hits and misses counters to zero.
 Resolver<K,V> setResolver(Resolver<K,V> resolver)
          Sets the resolver associated with this cache.
 int size()
          Returns the number of key->value mappings in the cache.
 int staticCapacity()
          Returns the capacity of the static 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
Default capacity of the static cache.

See Also:
Constant Field Values
Constructor Detail

StaticDynamicCache

public StaticDynamicCache(Cache<K,V> dynamicCache)
Constructor for the StaticDynamicCache object. Capacity of the static part of the cache is set to default and the resolver always returns null.

Parameters:
dynamicCache - dynamic cache to use

StaticDynamicCache

public StaticDynamicCache(Cache<K,V> dynamicCache,
                          int staticCapacity)
Constructor for the StaticDynamicCache object. Resolver always returns null.

Parameters:
dynamicCache - dynamic cache to use
staticCapacity - capacity of the static part of the cache

StaticDynamicCache

public StaticDynamicCache(Resolver<K,V> resolver,
                          Cache<K,V> dynamicCache)
Constructor for the StaticDynamicCache object. Capacity of the static part of the cache is set to default.

Parameters:
resolver - resolver to associate with the caches
dynamicCache - dynamic cache to use

StaticDynamicCache

public StaticDynamicCache(Resolver<K,V> resolver,
                          Cache<K,V> dynamicCache,
                          int staticCapacity)
Constructor for the StaticDynamicCache object.

Parameters:
resolver - resolver to associate with the caches
dynamicCache - dynamic cache to use
staticCapacity - capacity of the static part 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 dynamic cache according to its eviction policy.

Specified by:
put in interface Cache<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>
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>
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>
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 in static or dynamic cache.

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

clear

public void clear()
Remove all key->value mappings from the static and the dynamic cache.

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

keySet

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

Specified by:
keySet in interface Cache<K,V>
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>
Returns:
number of key->value mappings in the cache

capacity

public int capacity()
Returns the maximum number of key->value mappings cache can hold.

Specified by:
capacity in interface Cache<K,V>
Returns:
maximum capacity of the cache

staticCapacity

public int staticCapacity()
Returns the capacity of the static cache.

Returns:
capacity of the static cache

dynamicCapacity

public int dynamicCapacity()
Returns the capacity of the dynamic cache.

Returns:
capacity of the dynamic cache

nextEvicted

public K nextEvicted()
Gets the next key that would be removed from the dynamic cache according to the current cache replacement policy.

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

evict

public void evict()
Removes a key from the dynamic cache according to its cache eviction policy.

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

hitRatio

public float hitRatio()
Returns the hits/requests ratio in percents.

Specified by:
hitRatio in interface Cache<K,V>
Returns:
hit ratio of the cache

hits

public long hits()
Returns the number of all hits.

Specified by:
hits in interface Cache<K,V>
Returns:
number of all hits

misses

public long misses()
Returns the number of all misses.

Specified by:
misses in interface Cache<K,V>
Returns:
number of all misses

requests

public long requests()
Returns the number of all submitted requests

Specified by:
requests in interface Cache<K,V>
Returns:
number of all submitted requests

resetCounters

public void resetCounters()
Sets hits and misses counters to zero.

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

getResolver

public Resolver<K,V> getResolver()
Gets the current resolver associated with this cache.

Specified by:
getResolver in interface Cache<K,V>
Returns:
resolver associated with the cache

setResolver

public Resolver<K,V> setResolver(Resolver<K,V> resolver)
Sets the resolver associated with this cache.

Specified by:
setResolver in interface Cache<K,V>
Parameters:
resolver - new resolver we want to associate with this cache.
Returns:
replaced resolver

toString

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

Overrides:
toString in class java.lang.Object
Returns:
text representation of the cache