Product Documentation : Ehcache Developer Guide : Cache Eviction Algorithms : Plugging in Your own Eviction Algorithm
Plugging in Your own Eviction Algorithm
Ehcache allows you to plug in your own eviction algorithm using Cache.setMemoryStoreEvictionPolicy(Policy policy). You can utilize any Element metadata, which makes possible some very interesting approaches. For example, you might evict an element if it has been hit more than ten times.
* Sets the eviction policy strategy. The Cache will use a policy at startup.
* There are three policies which can be configured: LRU, LFU and FIFO. However
* many other policies are possible. That the policy has access to the whole
* element enables policies based on the key, value, metadata, statistics, or a
* combination of any of the above.
* It is safe to change the policy of a store at any time. The new policy takes
* effect immediately.
* @param policy the new policy
public void setMemoryStoreEvictionPolicy(Policy policy) {
A Policy must implement the following interface:
public interface Policy {
* @return the name of the Policy. Inbuilt examples are LRU, LFU and FIFO.
String getName();
* Finds the best eviction candidate based on the sampled elements. What
* distinguishes this approach from the classic data structures approach is
* that an Element contains metadata (e.g. usage statistics) which can be
* used for making policy decisions, while generic data structures do not.
* It is expected that implementations will take advantage of that
* metadata.
* @param sampledElements this should be a random subset of the population
* @param justAdded we probably never want to select the element just added.
* It is provided so that it can be ignored if selected. May be null.
* @return the selected Element
Element selectedBasedOnPolicy(Element[] sampledElements, Element justAdded);
* Compares the desirableness for eviction of two elements
* @param element1 the element to compare against
* @param element2 the element to compare
* @return true if the second element is preferable for eviction to the
* first element under ths policy
boolean compare(Element element1, Element element2);
Copyright © 2010-2015 Software AG, Darmstadt, Germany.

Product Logo |   Feedback