com.opencms.flex.util
Class CmsFlexLruCache

java.lang.Object
  |
  +--com.opencms.flex.util.CmsFlexLruCache

public class CmsFlexLruCache
extends 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 $
Author:
Thomas Weckert (t.weckert@alkacon.com)
See Also:
I_CmsFlexLruCacheObject

Constructor Summary
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(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.
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.
 void clear()
          Removes all cached objects in this cache.
protected  void finalize()
          Clears this cache for finalization.
 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.
 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.
 int size()
          Returns the count of all cached objects.
 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
 

Constructor Detail

CmsFlexLruCache

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

Parameters:
theMaxCacheCosts - the max. cache costs of all cached objects
theAvgCacheCosts - the avg. cache costs of all cached objects
theMaxObjectCosts - the max. allowed cache costs per object. Set theMaxObjectCosts to -1 if you don't want to limit the max. allowed cache costs per object
forceFinalization - should be true if a system wide garbage collection/finalization is forced after objects were removed from the cache

CmsFlexLruCache

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

Parameters:
theMaxCacheCosts - the max. cache costs of all cached objects
theAvgCacheCosts - the avg. cache costs of all cached objects
theMaxObjectCosts - the max. allowed cache costs per object. Set theMaxObjectCosts to -1 if you don't want to limit the max. allowed cache costs per object

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.

Parameters:
theMaxCacheCosts - the max. cache costs of all cached objects
theAvgCacheCosts - the avg. cache 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.

Parameters:
theMaxCacheCosts - the max. cache costs of all cached objects
theAvgCacheCosts - the avg. cache costs of all cached objects
forceFinalization - should be true if a system wide garbage collection/finalization is forced after objects were removed from the cache
Method Detail

toString

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

Overrides:
toString in class Object

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.

Parameters:
theCacheObject - the object being added to the cache
Returns:
true if the object was added to the cache, false if the object was denied because its cache costs were higher than the allowed max. cache costs per object

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.

Parameters:
theCacheObject - the object being touched

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.

Parameters:
theCacheObject - the object being removed from the list of all cached objects
Returns:
a reference to the object that was removed

size

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

Returns:
the count of all cached objects

finalize

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

Overrides:
finalize in class Object
Throwable

clear

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


getAvgCacheCosts

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

Returns:
the average costs of all cached objects

getMaxCacheCosts

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

Returns:
the max costs of all cached objects

getMaxObjectCosts

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

Returns:
the max allowed costs per cached object

getObjectCosts

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

Returns:
the current costs of all cached objects