Class CmsLruCache

java.lang.Object
org.opencms.cache.CmsLruCache

public class CmsLruCache extends Object
Implements an LRU (last recently used) cache.

The idea of this cache is 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_CmsLruCacheObject to be notified when they are added/removed from the CmsFlexLruCache.

Since:
6.0.0
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    CmsLruCache(long theMaxCacheCosts, long theAvgCacheCosts, int theMaxObjectCosts)
    The constructor with all options.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(I_CmsLruCacheObject theCacheObject)
    Adds a new object to this cache.
    void
    Removes all cached objects in this cache.
    long
    Returns the average costs of all cached objects.
    long
    Returns the max costs of all cached objects.
    int
    Returns the max allowed costs per cached object.
    int
    Returns the current costs of all cached objects.
    remove(I_CmsLruCacheObject theCacheObject)
    Removes an object from the list of all cached objects in this cache, no matter what position it has inside the list.
    int
    Returns the count of all cached objects.
    Returns a string representing the current state of the cache.
    boolean
    touch(I_CmsLruCacheObject 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, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • CmsLruCache

      public CmsLruCache(long theMaxCacheCosts, long theAvgCacheCosts, int theMaxObjectCosts)
      The constructor with all options.

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

    • add

      public boolean add(I_CmsLruCacheObject 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
    • clear

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

    • getAvgCacheCosts

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

      Returns:
      the average costs of all cached objects
    • getMaxCacheCosts

      public long 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
    • remove

      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
    • toString

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

      Overrides:
      toString in class Object
      Returns:
      a string representing the current state of the cache
    • touch

      public boolean touch(I_CmsLruCacheObject 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
      Returns:
      true if an object was found and touched