📄 cmsflexlrucache.java
字号:
else {
// case 3: the object is somewhere within the list, remove it to put it the front as the new head
theCacheObject.getPreviousLruObject().setNextLruObject( theCacheObject.getNextLruObject() );
theCacheObject.getNextLruObject().setPreviousLruObject( theCacheObject.getPreviousLruObject() );
}
// set the touched object as the new head in the linked list:
I_CmsFlexLruCacheObject oldHead = this.m_ListHead;
oldHead.setNextLruObject( theCacheObject );
theCacheObject.setNextLruObject( null );
theCacheObject.setPreviousLruObject( oldHead );
this.m_ListHead = theCacheObject;
return true;
}
/**
* 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_CmsFlexLruCacheObject theCacheObject ) {
// set the list pointers correct
if (this.m_ObjectCount>0) {
// there is at least 1 object already in the list
I_CmsFlexLruCacheObject oldHead = this.m_ListHead;
oldHead.setNextLruObject( theCacheObject );
theCacheObject.setPreviousLruObject( oldHead );
this.m_ListHead = theCacheObject;
}
else {
// it is the first object to be added to the list
this.m_ListTail = this.m_ListHead = theCacheObject;
theCacheObject.setPreviousLruObject( null );
}
theCacheObject.setNextLruObject( null );
// update cache stats. and notify the cached object
this.increaseCache( theCacheObject );
}
/**
* Removes an object from the list of all cached objects in this cache,
* no matter what position it has inside the list.<p>
*
* @param theCacheObject the object being removed from the list of all cached objects
* @return a reference to the object that was removed
*/
public synchronized I_CmsFlexLruCacheObject remove( I_CmsFlexLruCacheObject theCacheObject ) {
if (!this.isCached(theCacheObject)) {
// theCacheObject is null or not inside the cache
return null;
}
// set the list pointers correct
if (theCacheObject.getNextLruObject()==null) {
// remove the object from the head pos.
I_CmsFlexLruCacheObject newHead = theCacheObject.getPreviousLruObject();
if (newHead!=null) {
// if newHead is null, theCacheObject
// was the only object in the cache
newHead.setNextLruObject( null );
}
this.m_ListHead = newHead;
}
else if (theCacheObject.getPreviousLruObject()==null) {
// remove the object from the tail pos.
I_CmsFlexLruCacheObject newTail = theCacheObject.getNextLruObject();
if (newTail!=null) {
// if newTail is null, theCacheObject
// was the only object in the cache
newTail.setPreviousLruObject( null );
}
this.m_ListTail = newTail;
}
else {
// 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
this.decreaseCache( theCacheObject );
return theCacheObject;
}
/**
* Removes the tailing object from the list of all cached objects.<p>
*/
private synchronized void removeTail() {
I_CmsFlexLruCacheObject oldTail = null;
if ((oldTail=this.m_ListTail)!=null) {
I_CmsFlexLruCacheObject newTail = oldTail.getNextLruObject();
// set the list pointers correct
if (newTail!=null) {
// there are still objects remaining in the list
newTail.setPreviousLruObject( null );
this.m_ListTail = newTail;
}
else {
// we removed the last object from the list
this.m_ListTail = this.m_ListHead = null;
}
// update cache stats. and notify the cached object
this.decreaseCache( oldTail );
}
}
/**
* 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_CmsFlexLruCacheObject 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.
this.m_ObjectCosts -= theCacheObject.getLruCacheCosts();
this.m_ObjectCount--;
}
/**
* 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_CmsFlexLruCacheObject theCacheObject ) {
// notify the object that it was now added to the cache
//theCacheObject.notify();
theCacheObject.addToLruCache();
// update the cache stats.
this.m_ObjectCosts += theCacheObject.getLruCacheCosts();
this.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_CmsFlexLruCacheObject currentObject = this.m_ListTail;
while (currentObject!=null) {
if (this.m_ObjectCosts<this.m_AvgCacheCosts) break;
currentObject = currentObject.getNextLruObject();
this.removeTail();
}
if (m_ForceFinalization) {
// force a finalization/system garbage collection optionally
Runtime.getRuntime().runFinalization();
System.gc();
}
}
/**
* Returns the count of all cached objects.<p>
*
* @return the count of all cached objects
*/
public int size() {
return this.m_ObjectCount;
}
/**
* Clears this cache for finalization.<p>
*/
protected void finalize() throws java.lang.Throwable {
this.clear();
super.finalize();
}
/**
* Removes all cached objects in this cache.<p>
*/
public void clear() {
// remove all objects from the linked list from the tail to the head:
I_CmsFlexLruCacheObject currentObject = this.m_ListTail;
while (currentObject!=null) {
currentObject = currentObject.getNextLruObject();
this.removeTail();
}
// reset the data structure
this.m_ObjectCosts = this.m_ObjectCount = 0;
this.m_ListHead = this.m_ListTail = null;
if (m_ForceFinalization) {
// force a finalization/system garbage collection optionally
Runtime.getRuntime().runFinalization();
System.gc();
}
}
/**
* Returns the average costs of all cached objects.<p>
*
* @return the average costs of all cached objects
*/
public int getAvgCacheCosts() {
return this.m_AvgCacheCosts;
}
/**
* Returns the max costs of all cached objects.<p>
*
* @return the max costs of all cached objects
*/
public int getMaxCacheCosts() {
return this.m_MaxCacheCosts;
}
/**
* Returns the max allowed costs per cached object.<p>
*
* @return the max allowed costs per cached object
*/
public int getMaxObjectCosts() {
return this.m_MaxObjectCosts;
}
/**
* Returns the current costs of all cached objects.<p>
*
* @return the current costs of all cached objects
*/
public int getObjectCosts() {
return this.m_ObjectCosts;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -