org.osesb.utilities.cache
Class CacheEntry

java.lang.Object
  extended by org.osesb.utilities.cache.CacheEntry
All Implemented Interfaces:
java.lang.Comparable
Direct Known Subclasses:
TextFileCacheEntry

public class CacheEntry
extends java.lang.Object
implements java.lang.Comparable

CacheEntry wraps a cached Object in a SimpleCache. Thread-safe access to CacheEntry properties is the responsibility of SimpleCache.

CachEntry implements Comparable so that cache entries can be sorted by SimpleCache.evict(). For example, if sorted by accessCount, SimpleCache.evict() will remove items in ascending order of accessCount until SimpleCache.size() = SimpleCache.maxCacheSize.

CacheEntry properties that can be used to implement eviction strategies include:

1. CacheEntry.lastAccessedTime: evict based on least recently accessed.

2. CacheEntry.accessCount: evict based on least frequently accessed.

Since:
Version .9

Field Summary
 long accessCount
          The number of times that this object was accessed.
static java.lang.String ACCESSCOUNT_EVICTIONSTRATEGY
           
protected  java.lang.Object cachedObject
          The cached Object.
protected  java.lang.String cacheKey
          The cached Object's key.
protected  java.lang.reflect.Field compareField
          The Field for Comparable interface method compareTo().
 long lastAccessedTime
          The last time that this object was accessed.
static java.lang.String LASTACCESSTIME_EVICTIONSTRATEGY
           
protected static org.apache.commons.logging.Log log
          The logging implementation for this class.
protected  java.lang.String thisClassName
          The full name of this class.
protected  java.text.SimpleDateFormat toStringSDF
           
protected  java.util.Date toStringTime
           
 
Constructor Summary
CacheEntry(java.lang.String cacheKey, java.lang.Object cachedObject)
          Constructs a CacheEntry object
 
Method Summary
 int compareTo(java.lang.Object other)
          Compares this cacheEntry to another (Comparable Interface).
 long getAccessCount()
          Return the cache entry's access count.
 java.lang.Object getCachedObject()
          Return the cached Object.
 java.lang.String getCacheKey()
          Return the cached Object's key.
 java.lang.String getCompareFieldName()
          Return the cache entry's compareTo() field name.
 long getLastAccessedTime()
          Return the cache entry's lastAccessedTime stamp.
 void incrementAccessCount()
          Increment the cache entry's access count.
 void setAccessCount(long accessCount)
          Set the cache entry's access count.
 void setCompareField(java.lang.String compareFieldName)
          Set the cache entry's compareTo() field name.
 void setLastAccessedTime(long lastAccessedTime)
          Set the cache entry's lastAccessedTime stamp.
 java.lang.String toString()
          Returns the String representation of this CacheEntry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
The logging implementation for this class.


cachedObject

protected java.lang.Object cachedObject
The cached Object.


cacheKey

protected java.lang.String cacheKey
The cached Object's key. We need the cache key because SimpleCache.evict() iterates through cache entries in ascending order of the 'evictionStrategyField'. The evict() method needs access to CacheEntry keys in order to remove 'stale' CacheEntry(s).


lastAccessedTime

public long lastAccessedTime
The last time that this object was accessed.


accessCount

public long accessCount
The number of times that this object was accessed.


compareField

protected java.lang.reflect.Field compareField
The Field for Comparable interface method compareTo(). SimpleCache.put() sets this field according to the 'evictionStrategyField' element of the SimpleCache configuration.

'lastAccessedTime' and 'accessCount' can be used as eviction strategy fields. Eviction strategy fields must be 'public'.

SimpleCache.evict() sorts cache entries based on this field, and evicts entries in ascending order until the cache size equals maxCacheSize.


LASTACCESSTIME_EVICTIONSTRATEGY

public static java.lang.String LASTACCESSTIME_EVICTIONSTRATEGY

ACCESSCOUNT_EVICTIONSTRATEGY

public static java.lang.String ACCESSCOUNT_EVICTIONSTRATEGY

thisClassName

protected java.lang.String thisClassName
The full name of this class.


toStringSDF

protected java.text.SimpleDateFormat toStringSDF

toStringTime

protected java.util.Date toStringTime
Constructor Detail

CacheEntry

public CacheEntry(java.lang.String cacheKey,
                  java.lang.Object cachedObject)
           throws java.lang.Exception
Constructs a CacheEntry object

Parameters:
cachedObject - The object to cache.
Throws:
java.lang.Exception - if cachedObject is null.
Method Detail

getCachedObject

public java.lang.Object getCachedObject()
Return the cached Object.

Returns:
the cached Object.

getCacheKey

public java.lang.String getCacheKey()
Return the cached Object's key.

Returns:
the cached Object's key.

setLastAccessedTime

public void setLastAccessedTime(long lastAccessedTime)
Set the cache entry's lastAccessedTime stamp.

Parameters:
lastAccessedTime - The cache entry's lastAccessedTime stamp.

getLastAccessedTime

public long getLastAccessedTime()
Return the cache entry's lastAccessedTime stamp.

Returns:
the cache entry's lastAccessedTime stamp.

setAccessCount

public void setAccessCount(long accessCount)
Set the cache entry's access count.

Parameters:
accessCount - The cache entry's access count.

getAccessCount

public long getAccessCount()
Return the cache entry's access count.

Returns:
the cache entry's access count.

incrementAccessCount

public void incrementAccessCount()
Increment the cache entry's access count.


setCompareField

public void setCompareField(java.lang.String compareFieldName)
Set the cache entry's compareTo() field name. Useful for eviction strategy.

Parameters:
compareFieldName - The name of cache entry's compareTo() field.

getCompareFieldName

public java.lang.String getCompareFieldName()
Return the cache entry's compareTo() field name.

Returns:
the cache entry's compareTo() field name.

compareTo

public int compareTo(java.lang.Object other)
Compares this cacheEntry to another (Comparable Interface). compareTo() is used by the default eviction strategies.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - The CacheEntry for the comparison.
Returns:
an int consistent with comparison rules.

toString

public java.lang.String toString()
Returns the String representation of this CacheEntry.

Overrides:
toString in class java.lang.Object
Returns:
the String representation of this CacheEntry.