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
,UnboundedEJBObjectCache
public class BaseCache extends Object implements Cache
BaseCache Generic in-memory, abstract cache
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BaseCache.CacheItem
default CacheItem class implementation
-
Field Summary
Fields Modifier and Type Field Description protected Object[]
bucketLocks
protected BaseCache.CacheItem[]
buckets
protected int
entryCount
protected int
hitCount
protected ArrayList
listeners
protected int
maxBuckets
protected int
maxEntries
protected int
missCount
protected boolean[]
refreshFlags
protected int
removalCount
protected int
threshold
threshold for the cache; once the threshold is reached entries are removed to accomodate newer inserts
-
Constructor Summary
Constructors Constructor Description BaseCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Object
_put(int hashCode, Object key, Object value, int size, boolean addValue)
cache the given value at the specified key and return previous valueprotected BaseCache.CacheItem
_remove(int hashCode, Object key, Object value)
remove the item stored at the key.protected BaseCache.CacheItem
_removeItem(BaseCache.CacheItem ritem)
remove the item stored at the key.void
add(Object key, Object value)
add the given value to the cache at the specified keyvoid
add(Object key, Object value, int size)
add the given value with specified size to the cache at specified keyvoid
addCacheListener(CacheListener listener)
add the cache module listenerint
clear()
clear all the entries from the cache.void
clearStats()
clear the statsboolean
contains(Object key)
check if the cache contains the item at the keyprotected BaseCache.CacheItem
createItem(int hashCode, Object key, Object value, int size)
create new itemprotected void
decrementEntryCount()
void
destroy()
Sets all references to null.Enumeration
elements()
get an Enumeration for the keys stored in the cacheprotected boolean
eq(Object x, Object y)
Check for equality of non-null reference x and possibly-null y.Object
get(int hashCode, Object key)
get the item stored at the given pre-computed hash code and the key.Object
get(Object key)
get the item stored at the key.Iterator
getAll(Object key)
get all the items stored at the key.int
getEntryCount()
get the number of entries in the cacheprotected int
getIndex(int hashCode)
get the index of the item in the cacheint
getIndex(Object key)
get the index of the item given a keyObject
getStatByName(String key)
get the desired statistic counterMap
getStats()
get the stats snapshotprotected void
handleOverflow()
increase the thresholdprotected int
hash(Object x)
Returns a hash code for non-null Object x.protected void
incrementAddCount()
protected void
incrementEntryCount()
synchronized counter updatesprotected void
incrementHitCount()
protected void
incrementMissCount()
protected void
incrementOverflowCount()
protected void
incrementRefreshCount()
protected void
incrementRemovalCount()
void
init(int maxEntries, float loadFactor, Properties props)
initialize the cachevoid
init(int maxEntries, Properties props)
initialize the cacheboolean
isEmpty()
is this cache empty?protected boolean
isThresholdReached()
has cache reached its thresholdprotected void
itemAccessed(BaseCache.CacheItem item)
this item is accessedprotected BaseCache.CacheItem
itemAdded(BaseCache.CacheItem item)
this item is just added to the cacheprotected void
itemRefreshed(BaseCache.CacheItem item, int oldSize)
item value has been refreshedprotected void
itemRemoved(BaseCache.CacheItem item)
item value has been removed from the cacheIterator
keys()
get an Iterator for the keys stored in the cacheprotected Object
loadValue(Object key, int hashCode)
Cannot find an item with the given key and hashCodevoid
notifyRefresh(int index)
notify threads waiting for a refresh on the object associated with the keyObject
put(Object key, Object value)
/** cache the given value at the specified key and return previous valueObject
put(Object key, Object value, int size)
cache the given value at the specified key and return previous valueObject
remove(int hashCode, Object key)
remove the item stored at the key.Object
remove(Object key)
remove the item stored at the key.Object
remove(Object key, Object value)
remove the given value stored at the key; value-specific removals.void
removeAll(Object key)
remove all the item with the given key.void
trimExpiredEntries(int maxCount)
trim the expired entries from the cache.protected void
trimItem(BaseCache.CacheItem item)
trim the item from the cache and notify listenersIterator
values()
get an Iterator for the values stored in the cacheboolean
waitRefresh(int index)
wait for a refresh on the object associated with the key
-
-
-
Field Detail
-
maxEntries
protected int maxEntries
-
entryCount
protected int entryCount
-
threshold
protected int threshold
threshold 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
protected BaseCache.CacheItem[] buckets
-
bucketLocks
protected Object[] bucketLocks
-
refreshFlags
protected boolean[] refreshFlags
-
listeners
protected ArrayList listeners
-
-
Method Detail
-
init
public void init(int maxEntries, Properties props) throws Exception
initialize the cache
-
init
public void init(int maxEntries, float loadFactor, Properties props)
initialize the cache
-
addCacheListener
public void addCacheListener(CacheListener listener)
add the cache module listener- Specified by:
addCacheListener
in interfaceCache
- Parameters:
listener
-CacheListener
implementation
-
hash
protected int hash(Object x)
Returns a hash code for non-null Object x.
-
eq
protected boolean eq(Object x, Object y)
Check for equality of non-null reference x and possibly-null y.
-
handleOverflow
protected void handleOverflow()
increase the threshold
-
itemAdded
protected BaseCache.CacheItem itemAdded(BaseCache.CacheItem item)
this item is just added to the cache- Parameters:
item
-CacheItem
that 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
protected void itemAccessed(BaseCache.CacheItem item)
this item is accessed- Parameters:
item
-CacheItem
accessed Cache bucket is already synchronized by the caller
-
itemRefreshed
protected void itemRefreshed(BaseCache.CacheItem item, int oldSize)
item value has been refreshed- Parameters:
item
-CacheItem
that was refreshedoldSize
- size of the previous value that was refreshed Cache bucket is already synchronized by the caller
-
itemRemoved
protected void itemRemoved(BaseCache.CacheItem item)
item value has been removed from the cache- Parameters:
item
-CacheItem
that was just removed Cache bucket is already synchronized by the caller
-
loadValue
protected Object loadValue(Object key, int hashCode)
Cannot find an item with the given key and hashCode- Parameters:
key
-Object
that is not foundhashCode
-int
its hashCode
-
createItem
protected BaseCache.CacheItem createItem(int hashCode, Object key, Object value, int size)
create new item- Parameters:
hashCode
- for the entrykey
-Object
keyvalue
-Object
valuesize
- 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
public final int getIndex(Object key)
get the index of the item given a key
-
get
public Object get(int hashCode, Object key)
get the item stored at the given pre-computed hash code and the key.- Parameters:
key
- lookup key
-
contains
public boolean contains(Object key)
check if the cache contains the item at the key
-
keys
public Iterator keys()
get an Iterator for the keys stored in the cache
-
elements
public Enumeration elements()
get an Enumeration for the keys stored in the cache
-
values
public Iterator values()
get an Iterator for the values stored in the cache
-
put
public Object put(Object key, Object value)
/** cache the given value at the specified key and return previous value
-
put
public Object put(Object key, Object value, int size)
cache the given value at the specified key and return previous value
-
add
public void add(Object key, Object value)
add the given value to the cache at the specified key
-
add
public void add(Object key, Object value, int size)
add the given value with specified size to the cache at specified key
-
_put
protected Object _put(int hashCode, Object key, Object value, int size, boolean addValue)
cache the given value at the specified key and return previous value- Parameters:
hashCode
- previously computed hashCode for the keykey
- lookup keyobject
- item value to be storedsize
- in bytes of the value being cachedaddValue
- treate this operation to add (default is to replace)
-
remove
public Object remove(int hashCode, Object key)
remove the item stored at the key.- Parameters:
hashCode
- a precomputed hashCodekey
- lookup key
-
remove
public Object remove(Object key, Object value)
remove the given value stored at the key; value-specific removals.
-
_remove
protected BaseCache.CacheItem _remove(int hashCode, Object key, Object value)
remove the item stored at the key.- Parameters:
hashCode
- a precomputed hashCodekey
- lookup keyvalue
- of the item to be matched
-
_removeItem
protected BaseCache.CacheItem _removeItem(BaseCache.CacheItem ritem)
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
public void removeAll(Object key)
remove all the item with the given key.
-
trimItem
protected void trimItem(BaseCache.CacheItem item)
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:
waitRefresh
in interfaceCache
- Parameters:
key
- lookup key
-
notifyRefresh
public void notifyRefresh(int index)
notify threads waiting for a refresh on the object associated with the key- Specified by:
notifyRefresh
in interfaceCache
- Parameters:
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:
trimExpiredEntries
in 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:
getEntryCount
in interfaceCache
- Returns:
- the number of entries the cache currently holds
-
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
public Object getStatByName(String key)
get the desired statistic counter- Specified by:
getStatByName
in interfaceCache
- Parameters:
key
- to corresponding stat- Returns:
- an Object corresponding to the stat See also: Constant.java for the key
-
getStats
public Map 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:
clearStats
in interfaceCache
-
-