org.opengts.extra.util
Class MemCache<KEY,VAL>

java.lang.Object
  extended by org.opengts.extra.util.MemCache<KEY,VAL>
All Implemented Interfaces:
MemCacheAPI<KEY,VAL>

public class MemCache<KEY,VAL>
extends java.lang.Object
implements MemCacheAPI<KEY,VAL>

A simple memory cache handler


Nested Class Summary
 class MemCache.MemCacheMap<KEY,VAL>
          Class MemCachMap
static class MemCache.MemEntry<KEY,VAL>
          Class MemEntry
static class MemCache.MemEntryComparator<KEY,VAL>
          Class MemEntryComparator (used for debugging only)
 
Field Summary
static int DEFAULT_CACHE_SIZE
           
static double DEFAULT_TRIM_PERCENT
           
static int DEFAULT_TRIM_SIZE
           
static double MAX_MEMORY_UTILIZATION
           
static long MAXIMUM_CACHE_AGE_MS
           
static int MINIMUM_CACHE_SIZE
           
 
Constructor Summary
MemCache()
          Constructor
MemCache(int maxSize)
          Constructor
 
Method Summary
 void addValue(KEY key, VAL val)
          Add entry to cache
 boolean exceedsMaximumCacheSize(double maxGain)
          Returns true if the current size exceeds the maximum cache size
 int getMaximumCacheSize()
          Gets the maximum cache size
 int getMaximumCacheSizeCutbackCount()
          Returns the number of size cutbacks performed on the cache which were caused by excessive memory usage.
 long getMaximumEntryAgeMS()
          Gets the maximum age of a cached entry (in milliseconds)
 int getSize()
          Gets the current size of the memory-cache
 boolean getTrimOnAdd()
          Gets the trim-on-add state
 VAL getValue(KEY key)
          Get entry from cache
 VAL getValue(KEY key, VAL dft)
          Get entry from cache
 boolean hasValue(KEY key)
          Returns true if the specified key exists in the cache
static void main(java.lang.String[] argv)
           
 int setMaximumCacheSize(int maxSize)
          Sets the maximum cache size
 long setMaximumEntryAgeMS(long maxAgeMS)
          Sets the maximum age of a cached entry (in milliseconds)
 void setTrimOnAdd(boolean toa)
          Sets the trim-on-add state
 java.lang.String toString()
          Gets a String representation of the contents of this MemCache.
 void trimCache()
          Remove old/excessive entries in cache
 void trimCache(java.lang.String msg)
          Remove old/excessive entries in cache
static int TrimSize(int size)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MINIMUM_CACHE_SIZE

public static final int MINIMUM_CACHE_SIZE
See Also:
Constant Field Values

DEFAULT_CACHE_SIZE

public static final int DEFAULT_CACHE_SIZE
See Also:
Constant Field Values

MAXIMUM_CACHE_AGE_MS

public static final long MAXIMUM_CACHE_AGE_MS
See Also:
Constant Field Values

DEFAULT_TRIM_PERCENT

public static final double DEFAULT_TRIM_PERCENT
See Also:
Constant Field Values

DEFAULT_TRIM_SIZE

public static final int DEFAULT_TRIM_SIZE

MAX_MEMORY_UTILIZATION

public static final double MAX_MEMORY_UTILIZATION
See Also:
Constant Field Values
Constructor Detail

MemCache

public MemCache()
Constructor


MemCache

public MemCache(int maxSize)
Constructor

Method Detail

TrimSize

public static int TrimSize(int size)

getSize

public int getSize()
Gets the current size of the memory-cache

Returns:
The current size of the memory-cache

setMaximumCacheSize

public int setMaximumCacheSize(int maxSize)
Sets the maximum cache size

Specified by:
setMaximumCacheSize in interface MemCacheAPI<KEY,VAL>
Parameters:
maxSize - The maximum cache size
Returns:
The maximum cache size that was successfully set, or -1 if unable to set the size.

getMaximumCacheSize

public int getMaximumCacheSize()
Gets the maximum cache size

Specified by:
getMaximumCacheSize in interface MemCacheAPI<KEY,VAL>
Returns:
The maximum cache size, or -1 if unable to retrieve the size.

exceedsMaximumCacheSize

public boolean exceedsMaximumCacheSize(double maxGain)
Returns true if the current size exceeds the maximum cache size

Parameters:
maxGain - The gain/percentage to apply to the max cache size
Returns:
True if the current size exceeds the maximum size, multiplied by the maxGain.

setMaximumEntryAgeMS

public long setMaximumEntryAgeMS(long maxAgeMS)
Sets the maximum age of a cached entry (in milliseconds)

Parameters:
maxAgeMS - The maximum age of a cached entry (in milliseconds)
Returns:
The maximum age that was successfully set.

getMaximumEntryAgeMS

public long getMaximumEntryAgeMS()
Gets the maximum age of a cached entry (in milliseconds)

Returns:
The maximum age of a cached entry.

getMaximumCacheSizeCutbackCount

public int getMaximumCacheSizeCutbackCount()
Returns the number of size cutbacks performed on the cache which were caused by excessive memory usage.


setTrimOnAdd

public void setTrimOnAdd(boolean toa)
Sets the trim-on-add state


getTrimOnAdd

public boolean getTrimOnAdd()
Gets the trim-on-add state


trimCache

public void trimCache(java.lang.String msg)
Remove old/excessive entries in cache


trimCache

public void trimCache()
Remove old/excessive entries in cache


addValue

public void addValue(KEY key,
                     VAL val)
Add entry to cache

Specified by:
addValue in interface MemCacheAPI<KEY,VAL>
Parameters:
key - The key to add
val - The value to associate with the specified key

hasValue

public boolean hasValue(KEY key)
Returns true if the specified key exists in the cache

Specified by:
hasValue in interface MemCacheAPI<KEY,VAL>
Parameters:
key - The key to check for existence
Returns:
True if the specified key exists in the cache, false otherwise

getValue

public VAL getValue(KEY key)
Get entry from cache

Parameters:
key - The key of the value to retrieve
Returns:
The value for the specified key, or null if the key does not exist

getValue

public VAL getValue(KEY key,
                    VAL dft)
Get entry from cache

Specified by:
getValue in interface MemCacheAPI<KEY,VAL>
Parameters:
key - The key of the value to retrieve
dft - The default value if the key does not exist
Returns:
The value for the specified key, or specified default if the key does not exist

toString

public java.lang.String toString()
Gets a String representation of the contents of this MemCache. (used for debugging only)

Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] argv)