View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.store;
18  
19  import net.sf.ehcache.Element;
20  
21  /***
22   * An eviction policy.
23   * <p/>
24   * The Cache will use a policy at startup. There are three policy implementations provided in ehcache:
25   * LRU, LFU and FIFO. However many other policies are possible. That the policy
26   * has access to the whole element enables policies based on the key, value, metadata, statistics, or a combination of
27   * any of the above.
28   *
29   * @author Greg Luck
30   */
31  public interface Policy {
32  
33      /***
34       * @return the name of the Policy. Inbuilt examples are LRU, LFU and FIFO.
35       */
36      String getName();
37  
38      /***
39       * Finds the best eviction candidate based on the sampled elements. What distinguishes
40       * this approach from the classic data structures approach is that an Element contains
41       * metadata (e.g. usage statistics) which can be used for making policy decisions,
42       * while generic data structures do not. It is expected that implementations will take
43       * advantage of that metadata.
44       *
45       * @param sampledElements this should be a random subset of the population
46       * @param justAdded       we probably never want to select the element just added.
47       * It is provided so that it can be ignored if selected. May be null.
48       * @return the selected Element
49       */
50      Element selectedBasedOnPolicy(Element[] sampledElements, Element justAdded);
51  
52      /***
53       * Compares the desirableness for eviction of two elements
54       *
55       * @param element1 the element to compare against
56       * @param element2 the element to compare
57       * @return true if the second element is preferable for eviction to the first element
58       * under ths policy
59       */
60      boolean compare(Element element1, Element element2);
61  }