org.aris.cache.universalcache
Class UniversalCache

java.lang.Object
  extended by org.aris.cache.universalcache.UniversalCache

public class UniversalCache
extends java.lang.Object

A least used algorithm cache for complex key which caches maximum a constant number of objects based on usage. Used as a system wide object cache, this way making more efficient use of memory. Rather than caching each class in a different cache, cache all classes using this cache in order for most used classes to be cached and least used classes to be flushed, thus providing more memory for classes that are more often used. The cache has the ability to shrink or expand according to a hit/miss ratio. This way, less memory is used when the hit ratio is high, and more memory is reserved in order to make hit ratio high if it is low. (by default this behaviour is disabled, use setAllowResize(true) to enable after instantiation. ) The cache max size is guaranteed less or equal to that initially specified. The memory used by the cache is maximum max_allowed_items*sizeOf(max(sizeOf(class1),sizeOf(class2),...)).

Version:
1.1
Author:
Konstantine Kougios

Constructor Summary
UniversalCache(int cacheObjectsAverage)
          Creates a cache with a minimum of cacheObjectsAverage/2 and a maximum of 3 x cacheObjectsAverage /2 objects.
UniversalCache(int cacheObjectsMin, int cacheObjectsMax, float adjustFactor, float adjustFactorUnchanged, int dSize, boolean allowResize)
          Initialize the cache.
 
Method Summary
 void clear()
          Clears and initializes the cache.
 void dump(boolean printvalues)
          Dumps the cache to System.out.
 void flush(java.lang.Object[] startKey)
          Removes all cached items starting from startKey.
 void flushSynchronized(java.lang.Object[] startKey)
          Synchronized version of invalidateUnder.
 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
 java.lang.Object getSynchronized(java.lang.Object[] key)
          Gets the value assosiated to the key.
 boolean isAllowResize()
           
 void put(java.lang.Object[] key, java.lang.Object value)
          Insert/Update a key/value in the cache.
 void putSynchronized(java.lang.Object[] key, java.lang.Object value)
          Insert/Update a key/value in the cache.
 java.lang.Object remove(java.lang.Object[] key)
          Removes a key/value from the cache.
 java.lang.Object removeSynchronized(java.lang.Object[] key)
          Removes a key/value from the cache.
 void setAllowResize(boolean allowResize)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UniversalCache

public UniversalCache(int cacheObjectsMin,
                      int cacheObjectsMax,
                      float adjustFactor,
                      float adjustFactorUnchanged,
                      int dSize,
                      boolean allowResize)
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
See Also:
CacheCallBackI

UniversalCache

public UniversalCache(int cacheObjectsAverage)
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. allowResize is false, and the size of the cache remains to the cacheObjectsAverage, except if the allowResize is set to true.

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

clear

public void clear()
Clears and initializes the cache.


getHits

public int getHits()
Gets the cache hits

Returns:
Cache hits

getMisses

public int getMisses()
Gets the cache misses

Returns:
cache misses

putSynchronized

public void putSynchronized(java.lang.Object[] key,
                            java.lang.Object value)
Insert/Update a key/value in the cache. Synchronized by the 1st key (which is usually some .class)

Parameters:
key - The key
value - The value for this key

put

public void put(java.lang.Object[] key,
                java.lang.Object value)
Insert/Update a key/value in the cache.

Parameters:
key - The key
value - The value for this key

getSynchronized

public java.lang.Object getSynchronized(java.lang.Object[] key)
Gets the value assosiated to the key. Synchronized by the 1st key (which is usually some .class)

Parameters:
key - The key to search
Returns:
The value, or null if key not found

get

public java.lang.Object get(java.lang.Object[] key)
Gets the value assosiated to the key.

Parameters:
key - The key to search
Returns:
The value, or null if key not found

removeSynchronized

public java.lang.Object removeSynchronized(java.lang.Object[] key)
Removes a key/value from the cache. Synchronized by the 1st key.

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

remove

public java.lang.Object remove(java.lang.Object[] key)
Removes a key/value from the cache.

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

flushSynchronized

public void flushSynchronized(java.lang.Object[] startKey)
Synchronized version of invalidateUnder.

Parameters:
startKey - The starting key
See Also:
flush(Object[])

flush

public void flush(java.lang.Object[] startKey)
Removes all cached items starting from startKey.

Parameters:
startKey - The key to start. All subkeys are removed.

dump

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

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

isAllowResize

public boolean isAllowResize()
Returns:
true if the cache will adjust it's size according to hits/misses.

setAllowResize

public void setAllowResize(boolean allowResize)
Parameters:
allowResize - set to true to allow the cache to adjust it's size according to hits/misses ratio.