Package com.sun.appserv.util.cache
Class BaseCache
java.lang.Object
com.sun.appserv.util.cache.BaseCache
- All Implemented Interfaces:
Cache
- Direct Known Subclasses:
LruCache,MultiLruCache
BaseCache
Generic in-memory, abstract cache
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classdefault CacheItem class implementation -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Object[]protected BaseCache.CacheItem[]protected intprotected intprotected ArrayListprotected intprotected intprotected intprotected boolean[]protected intprotected intthreshold for the cache; once the threshold is reached entries are removed to accomodate newer inserts -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectcache the given value at the specified key and return previous valueprotected BaseCache.CacheItemremove the item stored at the key.protected BaseCache.CacheItem_removeItem(BaseCache.CacheItem ritem) remove the item stored at the key.voidadd the given value to the cache at the specified keyvoidadd the given value with specified size to the cache at specified keyvoidaddCacheListener(CacheListener listener) add the cache module listenerintclear()clear all the entries from the cache.voidclear the statsbooleancheck if the cache contains the item at the keyprotected BaseCache.CacheItemcreateItem(int hashCode, Object key, Object value, int size) create new itemprotected final voidvoiddestroy()Sets all references to null.elements()get an Enumeration for the keys stored in the cacheprotected booleanCheck for equality of non-null reference x and possibly-null y.get the item stored at the given pre-computed hash code and the key.get the item stored at the key.get all the items stored at the key.intget the number of entries in the cacheprotected final intgetIndex(int hashCode) get the index of the item in the cachefinal intget the index of the item given a keygetStatByName(String key) get the desired statistic countergetStats()get the stats snapshotprotected voidincrease the thresholdprotected intReturns a hash code for non-null Object x.protected final voidprotected final voidsynchronized counter updatesprotected final voidprotected final voidprotected final voidprotected final voidprotected final voidvoidinit(int maxEntries, float loadFactor, Properties props) initialize the cachevoidinit(int maxEntries, Properties props) initialize the cachebooleanisEmpty()is this cache empty?protected booleanhas cache reached its thresholdprotected voidthis item is accessedprotected BaseCache.CacheItemitemAdded(BaseCache.CacheItem item) this item is just added to the cacheprotected voiditemRefreshed(BaseCache.CacheItem item, int oldSize) item value has been refreshedprotected voiditem value has been removed from the cachekeys()get an Iterator for the keys stored in the cacheprotected ObjectCannot find an item with the given key and hashCodevoidnotifyRefresh(int index) notify threads waiting for a refresh on the object associated with the key/** cache the given value at the specified key and return previous valuecache the given value at the specified key and return previous valueremove the item stored at the key.remove the item stored at the key.remove the given value stored at the key; value-specific removals.voidremove all the item with the given key.voidtrimExpiredEntries(int maxCount) trim the expired entries from the cache.protected voidtrimItem(BaseCache.CacheItem item) trim the item from the cache and notify listenersvalues()get an Iterator for the values stored in the cachebooleanwaitRefresh(int index) wait for a refresh on the object associated with the key
-
Field Details
-
maxEntries
protected int maxEntries -
entryCount
protected int entryCount -
threshold
protected int thresholdthreshold for the cache; once the threshold is reached entries are removed to accomodate newer inserts -
hitCount
protected int hitCount -
missCount
protected int missCount -
removalCount
protected int removalCount -
maxBuckets
protected int maxBuckets -
buckets
-
bucketLocks
-
refreshFlags
protected boolean[] refreshFlags -
listeners
-
-
Constructor Details
-
BaseCache
public BaseCache()default constructor for the basic cache
-
-
Method Details
-
init
initialize the cache -
init
initialize the cache -
addCacheListener
add the cache module listener- Specified by:
addCacheListenerin interfaceCache- Parameters:
listener-CacheListenerimplementation
-
hash
Returns a hash code for non-null Object x. -
eq
Check for equality of non-null reference x and possibly-null y. -
handleOverflow
protected void handleOverflow()increase the threshold -
itemAdded
this item is just added to the cache- Parameters:
item-CacheItemthat was created- Returns:
- a overflow item; may be null Cache bucket is already synchronized by the caller Here, if cache is overflowing (i.e. reached threshold); this class simply makes the cache unbounded by raising the threshold. Subclasses are expected to provide a robust cache replacement algorithm. Subclasses should enhance this implemntation.
-
itemAccessed
this item is accessed- Parameters:
item-CacheItemaccessed Cache bucket is already synchronized by the caller
-
itemRefreshed
item value has been refreshed- Parameters:
item-CacheItemthat was refreshedoldSize- size of the previous value that was refreshed Cache bucket is already synchronized by the caller
-
itemRemoved
item value has been removed from the cache- Parameters:
item-CacheItemthat was just removed Cache bucket is already synchronized by the caller
-
loadValue
Cannot find an item with the given key and hashCode- Parameters:
key-Objectthat is not foundhashCode-intits hashCode
-
createItem
create new item- Parameters:
hashCode- for the entrykey-Objectkeyvalue-Objectvaluesize- size in bytes of the item subclasses may override to provide their own CacheItem extensions e.g. one that permits persistence.
-
isThresholdReached
protected boolean isThresholdReached()has cache reached its threshold- Returns:
- true when the cache reached its threshold
-
getIndex
protected final int getIndex(int hashCode) get the index of the item in the cache- Parameters:
hashCode- of the entry- Returns:
- the index to be used in the cache
-
getIndex
get the index of the item given a key -
get
get the item stored at the key. -
get
get the item stored at the given pre-computed hash code and the key.- Parameters:
key- lookup key
-
contains
check if the cache contains the item at the key -
getAll
get all the items stored at the key. -
keys
get an Iterator for the keys stored in the cache -
elements
get an Enumeration for the keys stored in the cache -
values
get an Iterator for the values stored in the cache -
put
/** cache the given value at the specified key and return previous value -
put
cache the given value at the specified key and return previous value -
add
add the given value to the cache at the specified key -
add
add the given value with specified size to the cache at specified key -
_put
cache the given value at the specified key and return previous value- Parameters:
hashCode- previously computed hashCode for the keykey- lookup keysize- in bytes of the value being cachedaddValue- treate this operation to add (default is to replace)object- item value to be stored
-
remove
remove the item stored at the key. -
remove
remove the item stored at the key.- Parameters:
hashCode- a precomputed hashCodekey- lookup key
-
remove
remove the given value stored at the key; value-specific removals. -
_remove
remove the item stored at the key.- Parameters:
hashCode- a precomputed hashCodekey- lookup keyvalue- of the item to be matched
-
_removeItem
remove the item stored at the key.- Parameters:
item- CacheItem to be removed- Returns:
- the item stored at the key; null if not found.
-
removeAll
remove all the item with the given key. -
trimItem
trim the item from the cache and notify listeners- Parameters:
item- to be trimmed
-
waitRefresh
public boolean waitRefresh(int index) wait for a refresh on the object associated with the key- Specified by:
waitRefreshin interfaceCache- Parameters:
index- index of the entry. The index must be obtained via one of thegetIndex()methods.key- lookup key
-
notifyRefresh
public void notifyRefresh(int index) notify threads waiting for a refresh on the object associated with the key- Specified by:
notifyRefreshin interfaceCache- Parameters:
index- index of the entry. The index must be obtained via one of thegetIndex()methods.key- lookup key
-
clear
public int clear()clear all the entries from the cache. -
trimExpiredEntries
public void trimExpiredEntries(int maxCount) trim the expired entries from the cache.- Specified by:
trimExpiredEntriesin interfaceCache- Parameters:
maxCount- maximum number of invalid entries to trim specify Integer.MAX_VALUE to trim all timedout entries This call is to be scheduled by a thread managed by the container.
-
getEntryCount
public int getEntryCount()get the number of entries in the cache- Specified by:
getEntryCountin interfaceCache- Returns:
- the number of entries the cache currently holds
-
isEmpty
public boolean isEmpty()is this cache empty? -
incrementEntryCount
protected final void incrementEntryCount()synchronized counter updates -
decrementEntryCount
protected final void decrementEntryCount() -
incrementHitCount
protected final void incrementHitCount() -
incrementMissCount
protected final void incrementMissCount() -
incrementRemovalCount
protected final void incrementRemovalCount() -
incrementRefreshCount
protected final void incrementRefreshCount() -
incrementAddCount
protected final void incrementAddCount() -
incrementOverflowCount
protected final void incrementOverflowCount() -
getStatByName
get the desired statistic counter- Specified by:
getStatByNamein interfaceCache- Parameters:
key- to corresponding stat- Returns:
- an Object corresponding to the stat See also: Constant.java for the key
-
getStats
get the stats snapshot -
destroy
public void destroy()Sets all references to null. This method should be called at the end of this object's life cycle. -
clearStats
public void clearStats()clear the stats- Specified by:
clearStatsin interfaceCache
-