public class BaseCache extends Object implements Cache
Modifier and Type | Class and Description |
---|---|
protected static class |
BaseCache.CacheItem
default CacheItem class implementation
|
Modifier and Type | Field and Description |
---|---|
protected static ResourceBundle |
_rb
The resource bundle containing the localized message strings.
|
protected Object[] |
bucketLocks |
protected BaseCache.CacheItem[] |
buckets |
protected int |
entryCount |
protected ArrayList |
listeners |
protected int |
maxBuckets |
protected boolean[] |
refreshFlags |
protected int |
threshold
threshold for the cache; once the threshold is reached
entries are removed to accomodate newer inserts
|
Constructor and Description |
---|
BaseCache()
default constructor for the basic cache
|
Modifier and Type | Method and 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 value
|
protected 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 key
|
void |
add(Object key,
Object value,
int size)
add the given value with specified size to the cache at specified key
|
void |
addCacheListener(CacheListener listener)
add the cache module listener
|
int |
clear()
clear all the entries from the cache.
|
void |
clearStats()
clear the stats
|
boolean |
contains(Object key)
check if the cache contains the item at the key
|
protected BaseCache.CacheItem |
createItem(int hashCode,
Object key,
Object value,
int size)
create new item
|
protected void |
decrementEntryCount() |
void |
destroy()
Sets all references to null.
|
Enumeration |
elements()
get an Enumeration for the keys stored in the cache
|
protected 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 cache
|
protected int |
getIndex(int hashCode)
get the index of the item in the cache
|
int |
getIndex(Object key)
get the index of the item given a key
|
Object |
getStatByName(String key)
get the desired statistic counter
|
Map |
getStats()
get the stats snapshot
|
protected void |
handleOverflow()
increase the threshold
|
protected int |
hash(Object x)
Returns a hash code for non-null Object x.
|
protected void |
incrementAddCount() |
protected void |
incrementEntryCount()
synchronized counter updates
|
protected void |
incrementHitCount() |
protected void |
incrementMissCount() |
protected void |
incrementOverflowCount() |
protected void |
incrementRefreshCount() |
protected void |
incrementRemovalCount() |
void |
init(int maxEntries,
float loadFactor,
Properties props)
initialize the cache
|
void |
init(int maxEntries,
Properties props)
initialize the cache
|
boolean |
isEmpty()
is this cache empty?
|
protected boolean |
isThresholdReached()
has cache reached its threshold
|
protected void |
itemAccessed(BaseCache.CacheItem item)
this item is accessed
|
protected BaseCache.CacheItem |
itemAdded(BaseCache.CacheItem item)
this item is just added to the cache
|
protected void |
itemRefreshed(BaseCache.CacheItem item,
int oldSize)
item value has been refreshed
|
protected void |
itemRemoved(BaseCache.CacheItem item)
item value has been removed from the cache
|
Iterator |
keys()
get an Iterator for the keys stored in the cache
|
protected Object |
loadValue(Object key,
int hashCode)
Cannot find an item with the given key and hashCode
|
void |
notifyRefresh(int index)
notify threads waiting for a refresh on the object associated with the key
|
Object |
put(Object key,
Object value)
/**
cache the given value at the specified key and return previous value
|
Object |
put(Object key,
Object value,
int size)
cache the given value at the specified key and return previous value
|
Object |
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 listeners
|
Iterator |
values()
get an Iterator for the values stored in the cache
|
boolean |
waitRefresh(int index)
wait for a refresh on the object associated with the key
|
protected static ResourceBundle _rb
protected int entryCount
protected int threshold
protected int maxBuckets
protected BaseCache.CacheItem[] buckets
protected Object[] bucketLocks
protected boolean[] refreshFlags
protected ArrayList listeners
public void init(int maxEntries, Properties props) throws Exception
public void init(int maxEntries, float loadFactor, Properties props)
public void addCacheListener(CacheListener listener)
addCacheListener
in interface Cache
listener
- CacheListener
implementationprotected int hash(Object x)
protected boolean eq(Object x, Object y)
protected void handleOverflow()
protected BaseCache.CacheItem itemAdded(BaseCache.CacheItem item)
item
- CacheItem
that was createdHere, 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.
protected void itemAccessed(BaseCache.CacheItem item)
item
- CacheItem
accessed
Cache bucket is already synchronized by the caller
protected void itemRefreshed(BaseCache.CacheItem item, int oldSize)
item
- CacheItem
that was refreshedoldSize
- size of the previous value that was refreshed
Cache bucket is already synchronized by the callerprotected void itemRemoved(BaseCache.CacheItem item)
item
- CacheItem
that was just removed
Cache bucket is already synchronized by the caller
protected Object loadValue(Object key, int hashCode)
key
- Object
that is not foundhashCode
- int
its hashCodeprotected BaseCache.CacheItem createItem(int hashCode, Object key, Object value, int size)
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.protected boolean isThresholdReached()
protected final int getIndex(int hashCode)
hashCode
- of the entrypublic final int getIndex(Object key)
public Object get(int hashCode, Object key)
key
- lookup keypublic boolean contains(Object key)
public Iterator keys()
public Enumeration elements()
public Iterator values()
public Object put(Object key, Object value)
public Object put(Object key, Object value, int size)
public void add(Object key, Object value, int size)
protected Object _put(int hashCode, Object key, Object value, int size, boolean addValue)
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)public Object remove(int hashCode, Object key)
hashCode
- a precomputed hashCodekey
- lookup keypublic Object remove(Object key, Object value)
protected BaseCache.CacheItem _remove(int hashCode, Object key, Object value)
hashCode
- a precomputed hashCodekey
- lookup keyvalue
- of the item to be matchedprotected BaseCache.CacheItem _removeItem(BaseCache.CacheItem ritem)
item
- CacheItem to be removedpublic void removeAll(Object key)
protected void trimItem(BaseCache.CacheItem item)
item
- to be trimmedpublic boolean waitRefresh(int index)
waitRefresh
in interface Cache
key
- lookup keypublic void notifyRefresh(int index)
notifyRefresh
in interface Cache
key
- lookup keypublic int clear()
public void trimExpiredEntries(int maxCount)
trimExpiredEntries
in interface Cache
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.
public int getEntryCount()
getEntryCount
in interface Cache
protected final void incrementEntryCount()
protected final void decrementEntryCount()
protected final void incrementHitCount()
protected final void incrementMissCount()
protected final void incrementRemovalCount()
protected final void incrementRefreshCount()
protected final void incrementAddCount()
protected final void incrementOverflowCount()
public Object getStatByName(String key)
getStatByName
in interface Cache
key
- to corresponding statpublic Map getStats()
public void destroy()
public void clearStats()
clearStats
in interface Cache
Copyright © 2006–2018 TinyGroup. All rights reserved.