org.aris.cache.idcache
Class IdCache

java.lang.Object
  extended by org.aris.cache.idcache.IdCache

public class IdCache
extends java.lang.Object

A least used cache which caches a max number of objects based on usage. Values must not be null. Callback functions are called in case of an put, get, remove

Author:
Konstantine Kougios
See Also:
CacheCallBackI

Constructor Summary
IdCache(int cacheObjectsAverage, CacheCallBackI cacheCallBack)
          Creates a cache with a minimum of cacheObjectsAverage/2 and a maximum of 3 x cacheObjectsAverage /2 objects.
IdCache(int cacheObjectsMin, int cacheObjectsMax, float adjustFactor, float adjustFactorUnchanged, int dSize, CacheCallBackI cacheCallBack)
          Initialize the cache.
 
Method Summary
 void addJustToCache(java.lang.Object key, java.lang.Object value)
          This just adds the key/value to the cache, without calling callbacks or checking if the key exists.
 void adjustMaxSize(int dSize)
          Adjust the size of the cache.
 void dump(boolean printvalues)
          Dumps the cache to System.out.
 java.lang.Object get(java.lang.Object key)
          Gets the value assosiated to the key.
 int getHits()
          Gets the cache hits
 int getMisses()
          Gets the cache misses
 void IdCache0(int cacheObjectsMin, int cacheObjectsMax, float adjustFactor, float adjustFactorUnchanged, int dSize, CacheCallBackI cacheCallBack)
           
 java.lang.Object insert(java.lang.Object key, java.lang.Object value)
          Insert a key/value in the cache/store.
 java.lang.Object remove(java.lang.Object key)
          Removes a key/value from the cache.
 java.lang.Object removeOnlyFromCache(java.lang.Object key)
          Removes this key only from the cache.
 boolean update(java.lang.Object key, java.lang.Object value)
          Update a key/value in the cache/store.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IdCache

public IdCache(int cacheObjectsMin,
               int cacheObjectsMax,
               float adjustFactor,
               float adjustFactorUnchanged,
               int dSize,
               CacheCallBackI cacheCallBack)
Initialize the cache.

Parameters:
cacheObjectsMin - The minimum number of objects that will be cached
cacheObjectsMax - The maximum number of objects that will be cached
adjustFactor - The formula hits/misses < adjustFactor - adjustFactorUnchanged decides wheather the cache will be increased , and the formula hits/misses > adjustFactor + adjustFactorUnchanged decides wheather the cache will be decreased (free memory). The increase/decrease are performed in dSize steps.
adjustFactorUnchanged - See the adjustFactor param
dSize - See the adjustFactor param
cacheCallBack - A callback class that will be called every time the cache needs to fetch/remove a key.
See Also:
CacheCallBackI

IdCache

public IdCache(int cacheObjectsAverage,
               CacheCallBackI cacheCallBack)
Creates a cache with a minimum of cacheObjectsAverage/2 and a maximum of 3 x cacheObjectsAverage /2 objects. The adjustFactor is 1.0, and the adjustFactorUnchanged is 0.5 . dSize equals to the 1/20 of the cacheObjectsAverage.

Parameters:
cacheObjectsAverage - The average number of objects that will be cached.
cacheCallBack - The callback.
See Also:
CacheCallBackI, IdCache(int, CacheCallBackI)
Method Detail

adjustMaxSize

public void adjustMaxSize(int dSize)
Adjust the size of the cache.

Parameters:
dSize - The dSize. It is added to the cache max size. If negative, and cacheSize>newCacheSize, all the extra elements will be removed.

getHits

public int getHits()
Gets the cache hits

Returns:
Cache hits

getMisses

public int getMisses()
Gets the cache misses

Returns:
cache misses

IdCache0

public void IdCache0(int cacheObjectsMin,
                     int cacheObjectsMax,
                     float adjustFactor,
                     float adjustFactorUnchanged,
                     int dSize,
                     CacheCallBackI cacheCallBack)
See Also:
IdCache

update

public boolean update(java.lang.Object key,
                      java.lang.Object value)
               throws java.lang.Exception
Update a key/value in the cache/store. The key shall exist (within cache or not).

Parameters:
key - The key to update
value - The new value for this key
Returns:
true if update was a success
Throws:
java.lang.Exception

insert

public java.lang.Object insert(java.lang.Object key,
                               java.lang.Object value)
                        throws java.lang.Exception
Insert a key/value in the cache/store. This will call the key1=callback.insertKey(key) and will use the key1 as the key.

Parameters:
key - The key to add, it must not already exist
value - The value for this key
Returns:
the key which is returned by cacheCallBack.insertkey()
Throws:
java.lang.Exception

addJustToCache

public void addJustToCache(java.lang.Object key,
                           java.lang.Object value)
This just adds the key/value to the cache, without calling callbacks or checking if the key exists.

Parameters:
key - The key
value - The value

get

public java.lang.Object get(java.lang.Object key)
                     throws java.lang.Exception
Gets the value assosiated to the key. The key is first searched in the cache and if it doesn't exists, the callback.findMissingKeyValue() is called. If the value is not null, the key/value is added to the cache, and returned.

Parameters:
key - The key to search
Returns:
The value
Throws:
java.lang.Exception

removeOnlyFromCache

public java.lang.Object removeOnlyFromCache(java.lang.Object key)
Removes this key only from the cache.

Parameters:
key - The key to remove
Returns:
The value assiciated with the key, or null if the key didn't exist in the cache

remove

public java.lang.Object remove(java.lang.Object key)
                        throws java.lang.Exception
Removes a key/value from the cache. If onlyFromCache, it removes the item from the cache without calling the CacheCallBack.removeKey().

Parameters:
key - The key to remove.
Returns:
The value if the key existed in the cache, or null if not.
Throws:
java.lang.Exception

dump

public void dump(boolean printvalues)
Dumps the cache to System.out. Usefull for debugginh.

Parameters:
printvalues - If true, it prints the values associated with the keys too.