📄 cmslrucache.java
字号:
// remove the object from within the list
theCacheObject.getPreviousLruObject().setNextLruObject(theCacheObject.getNextLruObject());
theCacheObject.getNextLruObject().setPreviousLruObject(theCacheObject.getPreviousLruObject());
}
// update cache stats. and notify the cached object
decreaseCache(theCacheObject);
return theCacheObject;
}
/**
* Returns the count of all cached objects.<p>
*
* @return the count of all cached objects
*/
public int size() {
return m_objectCount;
}
/**
* Returns a string representing the current state of the cache.<p>
* @return a string representing the current state of the cache
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("max. costs: " + m_maxCacheCosts).append(", ");
buf.append("avg. costs: " + m_avgCacheCosts).append(", ");
buf.append("max. costs/object: " + m_maxObjectCosts).append(", ");
buf.append("costs: " + m_objectCosts).append(", ");
buf.append("count: " + m_objectCount);
return buf.toString();
}
/**
* Touch an existing object in this cache, in the sense that it's "last-recently-used" state
* is updated.<p>
*
* @param theCacheObject the object being touched
* @return true if an object was found and touched
*/
public synchronized boolean touch(I_CmsLruCacheObject theCacheObject) {
if (!isCached(theCacheObject)) {
return false;
}
// only objects with cache costs < the max. allowed object cache costs can be cached!
if ((m_maxObjectCosts != -1) && (theCacheObject.getLruCacheCosts() > m_maxObjectCosts)) {
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(
Messages.LOG_CACHE_COSTS_TOO_HIGH_2,
new Integer(theCacheObject.getLruCacheCosts()),
new Integer(m_maxObjectCosts)));
}
remove(theCacheObject);
return false;
}
// set the list pointers correct
I_CmsLruCacheObject nextObj = theCacheObject.getNextLruObject();
if (nextObj == null) {
// case 1: the object is already at the head pos.
return true;
}
I_CmsLruCacheObject prevObj = theCacheObject.getPreviousLruObject();
if (prevObj == null) {
// case 2: the object at the tail pos., remove it from the tail to put it to the front as the new head
I_CmsLruCacheObject newTail = nextObj;
newTail.setPreviousLruObject(null);
m_listTail = newTail;
} else {
// case 3: the object is somewhere within the list, remove it to put it the front as the new head
prevObj.setNextLruObject(nextObj);
nextObj.setPreviousLruObject(prevObj);
}
// set the touched object as the new head in the linked list:
I_CmsLruCacheObject oldHead = m_listHead;
if (oldHead != null) {
oldHead.setNextLruObject(theCacheObject);
theCacheObject.setNextLruObject(null);
theCacheObject.setPreviousLruObject(oldHead);
}
m_listHead = theCacheObject;
return true;
}
/**
* Clears this cache for finalization.<p>
* @throws Throwable if something goes wring
*/
protected void finalize() throws Throwable {
try {
clear();
} catch (Throwable t) {
// ignore
}
super.finalize();
}
/**
* Adds a cache object as the new haed to the list of all cached objects in this cache.<p>
*
* @param theCacheObject the object being added as the new head to the list of all cached objects
*/
private void addHead(I_CmsLruCacheObject theCacheObject) {
// set the list pointers correct
if (m_objectCount > 0) {
// there is at least 1 object already in the list
I_CmsLruCacheObject oldHead = m_listHead;
oldHead.setNextLruObject(theCacheObject);
theCacheObject.setPreviousLruObject(oldHead);
m_listHead = theCacheObject;
} else {
// it is the first object to be added to the list
m_listTail = theCacheObject;
m_listHead = theCacheObject;
theCacheObject.setPreviousLruObject(null);
}
theCacheObject.setNextLruObject(null);
// update cache stats. and notify the cached object
increaseCache(theCacheObject);
}
/**
* Decrease this caches statistics
* and notify the cached object that it was removed from this cache.<p>
*
* @param theCacheObject the object being notified that it was removed from the cache
*/
private void decreaseCache(I_CmsLruCacheObject theCacheObject) {
// notify the object that it was now removed from the cache
//theCacheObject.notify();
theCacheObject.removeFromLruCache();
// set the list pointers to null
theCacheObject.setNextLruObject(null);
theCacheObject.setPreviousLruObject(null);
// update the cache stats.
m_objectCosts -= theCacheObject.getLruCacheCosts();
m_objectCount--;
}
/**
* Removes the last recently used objects from the list of all cached objects as long
* as the costs of all cached objects are higher than the allowed avg. costs of the cache.<p>
*/
private void gc() {
I_CmsLruCacheObject currentObject = m_listTail;
while (currentObject != null) {
if (m_objectCosts < m_avgCacheCosts) {
break;
}
currentObject = currentObject.getNextLruObject();
removeTail();
}
}
/**
* Increase this caches statistics
* and notify the cached object that it was added to this cache.<p>
*
* @param theCacheObject the object being notified that it was added to the cache
*/
private void increaseCache(I_CmsLruCacheObject theCacheObject) {
// notify the object that it was now added to the cache
//theCacheObject.notify();
theCacheObject.addToLruCache();
// update the cache stats.
m_objectCosts += theCacheObject.getLruCacheCosts();
m_objectCount++;
}
/**
* Test if a given object resides inside the cache.<p>
*
* @param theCacheObject the object to test
* @return true if the object is inside the cache, false otherwise
*/
private boolean isCached(I_CmsLruCacheObject theCacheObject) {
if (theCacheObject == null || m_objectCount == 0) {
// the cache is empty or the object is null (which is never cached)
return false;
}
I_CmsLruCacheObject nextObj = theCacheObject.getNextLruObject();
I_CmsLruCacheObject prevObj = theCacheObject.getPreviousLruObject();
if ((nextObj != null) || (prevObj != null)) {
// the object has either a predecessor or successor in the linked
// list of all cached objects, so it is inside the cache
return true;
}
if ((nextObj == null) && (prevObj == null)) {
if ((m_objectCount == 1)
&& (m_listHead != null)
&& (m_listTail != null)
&& m_listHead.equals(theCacheObject)
&& m_listTail.equals(theCacheObject)) {
// the object is the one and only object in the cache
return true;
}
}
return false;
}
/**
* Removes the tailing object from the list of all cached objects.<p>
*/
private synchronized void removeTail() {
I_CmsLruCacheObject oldTail = m_listTail;
if (oldTail != null) {
I_CmsLruCacheObject newTail = oldTail.getNextLruObject();
// set the list pointers correct
if (newTail != null) {
// there are still objects remaining in the list
newTail.setPreviousLruObject(null);
m_listTail = newTail;
} else {
// we removed the last object from the list
m_listTail = null;
m_listHead = null;
}
// update cache stats. and notify the cached object
decreaseCache(oldTail);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -