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 = new Integer(listSize);
else if (key.equals(Constants.STAT_LRUCACHE_TRIM_COUNT))
stat = new Integer(trimCount);
}
return stat;
|
public java.util.Map | getStats()
Map stats = super.getStats();
stats.put(Constants.STAT_LRUCACHE_LIST_LENGTH, new Integer(listSize));
stats.put(Constants.STAT_LRUCACHE_TRIM_COUNT, new Integer(trimCount));
return stats;
|
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
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() ) {
overflow = trimLru(lc.lastAccessed);
}
}
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 | setCacheName(java.lang.String name)
this.cacheName = name;
|
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) {
_logger.log(Level.WARNING,
"[" + cacheName + "]: trimLru(), resetting head and tail");
// do not let the tail go past the head
tail = head = null;
} else {
tail.lNext = null;
}
} else {
tail = head = null;
}
if (trimItem != null) {
trimItem.isTrimmed = true;
trimItem.lPrev = null;
trimCount++;
listSize--;
}
return trimItem;
|