KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > util > AbstractPolicyCache


1 package org.shiftone.cache.util;
2
3
4
5 import org.shiftone.cache.util.reaper.ReapableCache;
6
7
8 /**
9  * Class AbstractPolicyCache
10  *
11  *
12  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
13  * @version $Revision: 1.5 $
14  */

15 public abstract class AbstractPolicyCache implements ReapableCache
16 {
17
18     private static final double MIN_PCT_FREE = 1.0;
19     private static final int SHRINK_MULT = 2;
20     public static final int SHRINK_DIV = 3;
21     private final String JavaDoc name;
22     private final int maxSize;
23     private final long timeoutMilliSeconds;
24
25     protected AbstractPolicyCache(String JavaDoc name, long timeoutMilliSeconds, int maxSize)
26     {
27
28         this.name = name;
29         this.timeoutMilliSeconds = timeoutMilliSeconds;
30         this.maxSize = maxSize;
31     }
32
33
34     public final void addObject(Object JavaDoc userKey, Object JavaDoc cacheObject)
35     {
36
37         CacheNode node;
38
39
40
41         node = findNodeByKey(userKey);
42
43         if (node != null)
44         {
45
46             // if the node exists, then set it's value, and revalue it.
47
// this is better than deleting it, because it doesn't require
48
// more memory to be allocated
49
node.setValue(cacheObject);
50             revalueNode(node);
51         }
52         else
53         {
54             shrinkToSize(getMaxSize() - 1);
55             createNode(userKey, cacheObject);
56         }
57
58         removeExpiredElements();
59
60         //checkFreeMemory();
61
}
62
63
64     public final Object JavaDoc getObject(Object JavaDoc key)
65     {
66
67         Object JavaDoc value = null;
68         CacheNode node;
69
70         removeExpiredElements();
71
72         node = findNodeByKey(key);
73
74         if (node == null)
75         {
76             ; // cache miss
77
}
78         else if (node.isExpired())
79         {
80             delete(node);
81         }
82         else if (node != null)
83         {
84             revalueNode(node);
85
86             value = node.getValue();
87         }
88
89         return value;
90     }
91
92
93     public final void remove(Object JavaDoc key)
94     {
95
96         CacheNode node = findNodeByKey(key);
97
98         if (node != null)
99         {
100             delete(node);
101         }
102
103         removeExpiredElements();
104     }
105
106
107     protected String JavaDoc getName()
108     {
109         return name;
110     }
111
112
113     protected final int getMaxSize()
114     {
115         return this.maxSize;
116     }
117
118
119     protected final long getTimeoutMilliSeconds()
120     {
121         return this.timeoutMilliSeconds;
122     }
123
124
125     /**
126      * Method removes the least valuable nodes, one by one until the cache's
127      * size is less than or equal to the desired size.
128      */

129     private final void shrinkToSize(int desiredSize)
130     {
131
132         while (size() > desiredSize)
133         {
134
135             //Log.info(getClass(), "desiredSize = " + desiredSize + " size=" + size());
136
removeLeastValuableNode();
137         }
138     }
139
140
141     /**
142      * This method calls shrinkToSize(0) which will loop through
143      * each element, removing them one by one (in order of lease valued
144      * to most valued). Derived classes may wish to implement this in a
145      * more efficient way (by just reinitalizing itself).
146      */

147     public void clear()
148     {
149         shrinkToSize(0);
150     }
151
152
153     /**
154      * Method checkFreeMemory
155      */

156     private final void checkFreeMemory()
157     {
158
159         /*
160       double percentFree = MemUtil.freeMemoryPct();
161       long start = System.currentTimeMillis();
162       DecimalFormat format = null;
163
164       if (percentFree < MIN_PCT_FREE)
165       {
166
167           //log.warn("xxx", new Exception("XXX"));
168           format = new DecimalFormat("##.##");
169
170           shrinkToSize((size() * SHRINK_MULT) / SHRINK_DIV);
171           Runtime.getRuntime().gc();
172           Runtime.getRuntime().runFinalization();
173           Thread.yield();
174           log.warn(format.format(percentFree) + "% memory free - cleanup took "
175                    + (System.currentTimeMillis() - start) + "ms - size is now " + size());
176       }
177      */

178     }
179
180
181     /**
182      * Method findNodeByKey
183      */

184     abstract protected CacheNode findNodeByKey(Object JavaDoc key);
185
186
187     /**
188      * Update the node's value. This is done immediately after
189      * the node is retrieved.
190      */

191     abstract protected void revalueNode(CacheNode node);
192
193
194     /**
195      * Remove a node from the cache.
196      */

197     abstract protected void delete(CacheNode node);
198
199
200     /**
201      * This method will execute the cache's eviction strategy.
202      * If this method is called, there will be at least one
203      * element in the cache to remove. The method itself does
204      * not need to check for the existance of a element.
205      * <p>
206      * This method is only called by shrinkToSize();
207      */

208     abstract protected void removeLeastValuableNode();
209
210
211     /**
212      * Purge the cache of expired elements.
213      */

214     abstract public void removeExpiredElements();
215
216
217     /**
218      * Create a new node.
219      */

220     abstract protected CacheNode createNode(Object JavaDoc userKey, Object JavaDoc cacheObject);
221
222
223     private static String JavaDoc shortName(Class JavaDoc klass)
224     {
225
226         String JavaDoc className = klass.getName();
227         int lastDot = className.lastIndexOf('.');
228
229         if (lastDot != -1)
230         {
231             className = className.substring(lastDot + 1);
232         }
233
234         return className;
235     }
236
237
238     public String JavaDoc toString()
239     {
240         return shortName(getClass()) + "(" + getName() + "," + getTimeoutMilliSeconds() + "," + getMaxSize() + ")";
241     }
242 }
243
Popular Tags