Methods Summary |
---|
protected CacheItem | createItem(int hashCode, java.lang.Object key, java.lang.Object value, int size)create new item
return new LruCacheItem(hashCode, key, value, size);
|
public java.lang.Object | getStatByName(java.lang.String key)get the desired statistic counter
Object stat = super.getStatByName(key);
if (stat == null && key != null) {
if (key.equals(Constants.STAT_LRUCACHE_LIST_LENGTH))
stat = Integer.valueOf(listSize);
else if (key.equals(Constants.STAT_LRUCACHE_TRIM_COUNT))
stat = Integer.valueOf(trimCount);
}
return stat;
|
public java.util.Map | getStats()
Map stats = super.getStats();
stats.put(Constants.STAT_LRUCACHE_LIST_LENGTH,
Integer.valueOf(listSize));
stats.put(Constants.STAT_LRUCACHE_TRIM_COUNT,
Integer.valueOf(trimCount));
return stats;
|
public void | init(int maxEntries, long timeout, float loadFactor, java.util.Properties props)initialize the cache
// if the max entries is <= 0 then set the default max entries
if (maxEntries <= 0) {
maxEntries = defaultMaxEntries;
// mark this cache unbounded
isUnbounded = true;
}
setTimeout(timeout);
super.init(maxEntries, loadFactor, props);
|
protected void | itemAccessed(CacheItem item)this item is accessed
LruCacheItem lc = (LruCacheItem) item;
synchronized (this) {
// if the item is already trimmed from the LRU list, nothing to do.
if (lc.isTrimmed)
return;
// update the timestamp
lc.lastAccessed = System.currentTimeMillis();
LruCacheItem prev = lc.lPrev;
LruCacheItem next = lc.lNext;
// update the LRU list
if (prev != null) {
// put the item at the head of LRU list
lc.lPrev = null;
lc.lNext = head;
head.lPrev = lc;
head = lc;
// patch up the previous neighbors
prev.lNext = next;
if (next != null)
next.lPrev = prev;
else
tail = prev;
}
}
|
protected CacheItem | itemAdded(CacheItem item)/**
this item is just added to the cache
boolean updateThreshold = false;
CacheItem overflow = null;
LruCacheItem lc = (LruCacheItem) item;
// set the timestamp
lc.lastAccessed = System.currentTimeMillis();
// update the LRU
synchronized (this) {
if (head != null) {
head.lPrev = lc;
lc.lNext = head;
lc.lPrev = null;
head = lc;
}
else {
head = tail = lc;
lc.lPrev = lc.lNext = null;
}
listSize++;
if (isThresholdReached()) {
if (!isUnbounded)
overflow = trimLru(lc.lastAccessed);
else
updateThreshold = true;
}
}
// update the base cache threshold if needed
if (updateThreshold)
super.handleOverflow();
return overflow;
|
protected void | itemRefreshed(CacheItem item, int oldSize)item value has been refreshed
itemAccessed(item);
|
protected void | itemRemoved(CacheItem item)item value has been removed from the cache
LruCacheItem l = (LruCacheItem) item;
// remove the item from the LRU list
synchronized (this) {
LruCacheItem prev = l.lPrev;
LruCacheItem next = l.lNext;
// if the item is already trimmed from the LRU list, nothing to do.
if (l.isTrimmed)
return;
// patch up the neighbors and make sure head/tail are correct
if (prev != null)
prev.lNext = next;
else
head = next;
if (next != null)
next.lPrev = prev;
else
tail = prev;
l.lPrev = l.lNext = null;
listSize--;
}
|
public void | setTimeout(long timeout)sets the timeout value
// accept a positive timeout
if (timeout > 0)
this.timeout = timeout;
|
public void | trimExpiredEntries(int maxCount)trim the expired entries from the cache.
int count = 0;
LruCacheItem item;
long currentTime = System.currentTimeMillis();
ArrayList list = new ArrayList();
synchronized (this) {
// traverse LRU list till we reach a valid item; remove them at once
for (item = tail; item != null && count < maxCount;
item = item.lPrev) {
if (timeout != NO_TIMEOUT &&
(item.lastAccessed + timeout) <= currentTime) {
item.isTrimmed = true;
list.add(item);
count++;
} else
break;
}
// if there was at least one invalid item then item != tail.
if (item != tail) {
if (item != null)
item.lNext = null;
else
head = null;
tail = item;
}
listSize -= count;
trimCount += count;
}
// trim the items from the BaseCache from the old tail backwards
for (int index=list.size()-1; index >= 0; index--) {
trimItem((LruCacheItem) list.get(index));
}
|
protected CacheItem | trimLru(long currentTime)trim one item from the LRU list
LruCacheItem trimItem = tail;
if (tail != head) {
tail = trimItem.lPrev;
if(tail == null) {
//TODO:: print out a warning message here
tail = head = null;
} else {
tail.lNext = null;
trimItem.lPrev = null;
}
} else {
tail = head = null;
}
if (trimItem != null) {
trimItem.isTrimmed = true;
trimItem.lPrev = null;
trimCount++;
listSize--;
}
return trimItem;
|