Class CmsLruCache
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
ConstructorDescriptionCmsLruCache
(long theMaxCacheCosts, long theAvgCacheCosts, int theMaxObjectCosts) The constructor with all options. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(I_CmsLruCacheObject theCacheObject) Adds a new object to this cache.void
clear()
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
size()
Returns the count of all cached objects.toString()
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.
-
Constructor Details
-
CmsLruCache
The constructor with all options.- Parameters:
theMaxCacheCosts
- the maximum cache costs of all cached objectstheAvgCacheCosts
- the average cache costs of all cached objectstheMaxObjectCosts
- 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
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
Removes all cached objects in this cache. -
getAvgCacheCosts
Returns the average costs of all cached objects.- Returns:
- the average costs of all cached objects
-
getMaxCacheCosts
Returns the max costs of all cached objects.- Returns:
- the max costs of all cached objects
-
getMaxObjectCosts
Returns the max allowed costs per cached object.- Returns:
- the max allowed costs per cached object
-
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
Returns the count of all cached objects.- Returns:
- the count of all cached objects
-
toString
Returns a string representing the current state of the cache. -
touch
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
-