Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.opencms.flex.util
Class CmsFlexLruCache  view CmsFlexLruCache download CmsFlexLruCache.java

java.lang.Object
  extended bycom.opencms.flex.util.CmsFlexLruCache

public class CmsFlexLruCache
extends java.lang.Object

Implements an LRU (last recently used) cache.

The idea of this cache to separate the caching policy from the data structure where the cached objects are stored. The advantage of doing so is, that the CmsFlexLruCache can identify the last-recently-used object in O(1), whereas you would need at least O(n) to traverse the data structure that stores the cached objects. Second, you can easily use the CmsFlexLruCache to get an LRU cache, no matter what data structure is used to store your objects.

The cache policy is affected by the "costs" of the objects being cached. Valuable cache costs might be the byte size of the cached objects for example.

To add/remove cached objects from the data structure that stores them, the objects have to implement the methods defined in the interface I_CmsFlexLruCacheObject to be notified when they are added/removed from the CmsFlexLruCache.

Version:
$Revision: 1.13 $

Field Summary
private  int m_AvgCacheCosts
          The avg.
private  boolean m_ForceFinalization
          Force a finalization after down-sizing the cache?
private  I_CmsFlexLruCacheObject m_ListHead
          The head of the list of double linked LRU cache objects.
private  I_CmsFlexLruCacheObject m_ListTail
          The tail of the list of double linked LRU cache objects.
private  int m_MaxCacheCosts
          The max.
private  int m_MaxObjectCosts
          The max.
private  int m_ObjectCosts
          The costs of all cached objects.
private  int m_ObjectCount
          The sum of all cached objects.
 
Constructor Summary
CmsFlexLruCache(int theMaxCacheCosts, int theAvgCacheCosts)
          Constructor for a LRU cache with forced garbage collection/finalization, the max.
CmsFlexLruCache(int theMaxCacheCosts, int theAvgCacheCosts, boolean forceFinalization)
          Constructor for a LRU cache where the max.
CmsFlexLruCache(int theMaxCacheCosts, int theAvgCacheCosts, int theMaxObjectCosts)
          Constructor for a LRU cache with forced garbage collection/finalization.
CmsFlexLruCache(int theMaxCacheCosts, int theAvgCacheCosts, int theMaxObjectCosts, boolean forceFinalization)
          The constructor with all options.
 
Method Summary
 boolean add(I_CmsFlexLruCacheObject theCacheObject)
          Adds a new object to this cache.
private  void addHead(I_CmsFlexLruCacheObject theCacheObject)
          Adds a cache object as the new haed to the list of all cached objects in this cache.
 void clear()
          Removes all cached objects in this cache.
private  void decreaseCache(I_CmsFlexLruCacheObject theCacheObject)
          Decrease this caches statistics and notify the cached object that it was removed from this cache.
protected  void finalize()
          Clears this cache for finalization.
private  void gc()
          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.
 int getAvgCacheCosts()
          Returns the average costs of all cached objects.
 int getMaxCacheCosts()
          Returns the max costs of all cached objects.
 int getMaxObjectCosts()
          Returns the max allowed costs per cached object.
 int getObjectCosts()
          Returns the current costs of all cached objects.
private  void increaseCache(I_CmsFlexLruCacheObject theCacheObject)
          Increase this caches statistics and notify the cached object that it was added to this cache.
private  boolean isCached(I_CmsFlexLruCacheObject theCacheObject)
          Test if a given object resides inside the cache.
 I_CmsFlexLruCacheObject remove(I_CmsFlexLruCacheObject theCacheObject)
          Removes an object from the list of all cached objects in this cache, no matter what position it has inside the list.
private  void removeTail()
          Removes the tailing object from the list of all cached objects.
 int size()
          Returns the count of all cached objects.
 java.lang.String toString()
          Returns a string representing the current state of the cache.
 boolean touch(I_CmsFlexLruCacheObject theCacheObject)
          Touch an existing object in this cache, in the sense that it's "last-recently-used" state is updated.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_ListHead

private I_CmsFlexLruCacheObject m_ListHead
The head of the list of double linked LRU cache objects.


m_ListTail

private I_CmsFlexLruCacheObject m_ListTail
The tail of the list of double linked LRU cache objects.


m_ForceFinalization

private boolean m_ForceFinalization
Force a finalization after down-sizing the cache?


m_MaxCacheCosts

private int m_MaxCacheCosts
The max. sum of costs the cached objects might reach.


m_AvgCacheCosts

private int m_AvgCacheCosts
The avg. sum of costs the cached objects.


m_MaxObjectCosts

private int m_MaxObjectCosts
The max. costs of cacheable objects.


m_ObjectCosts

private int m_ObjectCosts
The costs of all cached objects.


m_ObjectCount

private int m_ObjectCount
The sum of all cached objects.

Constructor Detail

CmsFlexLruCache

public CmsFlexLruCache(int theMaxCacheCosts,
                       int theAvgCacheCosts,
                       int theMaxObjectCosts,
                       boolean forceFinalization)
The constructor with all options.


CmsFlexLruCache

public CmsFlexLruCache(int theMaxCacheCosts,
                       int theAvgCacheCosts,
                       int theMaxObjectCosts)
Constructor for a LRU cache with forced garbage collection/finalization.


CmsFlexLruCache

public CmsFlexLruCache(int theMaxCacheCosts,
                       int theAvgCacheCosts)
Constructor for a LRU cache with forced garbage collection/finalization, the max. allowed costs of cacheable objects is 1/4 of the max. costs of all cached objects.


CmsFlexLruCache

public CmsFlexLruCache(int theMaxCacheCosts,
                       int theAvgCacheCosts,
                       boolean forceFinalization)
Constructor for a LRU cache where the max. allowed costs of cacheable objects is 1/4 of the max. costs of all cached objects.

Method Detail

toString

public java.lang.String toString()
Returns a string representing the current state of the cache.


add

public boolean add(I_CmsFlexLruCacheObject theCacheObject)
Adds a new object to this cache.

If add the same object more than once, the object is touched instead.


isCached

private boolean isCached(I_CmsFlexLruCacheObject theCacheObject)
Test if a given object resides inside the cache.


touch

public boolean touch(I_CmsFlexLruCacheObject theCacheObject)
Touch an existing object in this cache, in the sense that it's "last-recently-used" state is updated.


addHead

private void addHead(I_CmsFlexLruCacheObject theCacheObject)
Adds a cache object as the new haed to the list of all cached objects in this cache.


remove

public I_CmsFlexLruCacheObject remove(I_CmsFlexLruCacheObject theCacheObject)
Removes an object from the list of all cached objects in this cache, no matter what position it has inside the list.


removeTail

private void removeTail()
Removes the tailing object from the list of all cached objects.


decreaseCache

private void decreaseCache(I_CmsFlexLruCacheObject theCacheObject)
Decrease this caches statistics and notify the cached object that it was removed from this cache.


increaseCache

private void increaseCache(I_CmsFlexLruCacheObject theCacheObject)
Increase this caches statistics and notify the cached object that it was added to this cache.


gc

private void gc()
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.


size

public int size()
Returns the count of all cached objects.


finalize

protected void finalize()
                 throws java.lang.Throwable
Clears this cache for finalization.


clear

public void clear()
Removes all cached objects in this cache.


getAvgCacheCosts

public int getAvgCacheCosts()
Returns the average costs of all cached objects.


getMaxCacheCosts

public int getMaxCacheCosts()
Returns the max costs of all cached objects.


getMaxObjectCosts

public int getMaxObjectCosts()
Returns the max allowed costs per cached object.


getObjectCosts

public int getObjectCosts()
Returns the current costs of all cached objects.