KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > LFUAlgorithm


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.eviction;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Region;
12
13 /**
14  * Least Frequently Used algorithm for cache eviction.
15  * Note that this algorithm is not thread-safe.
16  * <p/>
17  * This algorithm relies on maxNodes and minNodes to operate correctly.
18  * Eviction takes place using Least Frequently Used algorithm. A node A
19  * that is used less than a node B is evicted sooner.
20  * <p/>
21  * The minNodes property defines a threshold for eviction. If minNodes = 100,
22  * the LFUAlgorithm will not evict the cache to anything less than 100 elements
23  * still left in cache. The maxNodes property defines the maximum number of nodes
24  * the cache will accept before eviction. maxNodes = 0 means that this region is
25  * unbounded. minNodes = 0 means that the eviction queue will attempt to bring
26  * the cache of this region to 0 elements (evict all elements) whenever it is run.
27  * <p/>
28  * This algorithm uses a sorted eviction queue. The eviction queue is sorted in
29  * ascending order based on the number of times a node is visited. The more frequently
30  * a node is visited, the less likely it will be evicted.
31  *
32  * @author Daniel Huang - dhuang@jboss.org 10/2005
33  * @version $Revision: 1.7 $
34  */

35 public class LFUAlgorithm extends BaseSortedEvictionAlgorithm implements EvictionAlgorithm
36 {
37    private static final Log log = LogFactory.getLog(LFUAlgorithm.class);
38
39
40    public LFUAlgorithm()
41    {
42       super();
43    }
44
45    protected boolean shouldEvictNode(NodeEntry ne)
46    {
47       if (log.isTraceEnabled())
48       {
49          log.trace("Deciding whether node in queue " + ne.getFqn() + " requires eviction.");
50       }
51
52       LFUConfiguration config = (LFUConfiguration) region.getEvictionPolicyConfig();
53       int size = this.getEvictionQueue().getNumberOfNodes();
54       if (config.getMaxNodes() != 0 && size > config.getMaxNodes())
55       {
56          return true;
57       }
58       else if (size > config.getMinNodes())
59       {
60          return true;
61       }
62
63       return false;
64    }
65
66    /**
67     * Will create a LFUQueue to be used as the underlying eviction queue.
68     *
69     * @param region MarshRegion to create the eviction queue for.
70     * @return The created LFUQueue.
71     * @throws EvictionException
72     */

73    protected EvictionQueue setupEvictionQueue(Region region) throws EvictionException
74    {
75       return new LFUQueue();
76    }
77
78    protected void prune() throws EvictionException
79    {
80       super.prune();
81
82       // clean up the Queue's eviction removals
83
((LFUQueue) this.evictionQueue).prune();
84    }
85 }
86
Popular Tags