KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > cache


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module;
11
12 import java.util.*;
13
14 import org.mmbase.util.LRUHashtable;
15
16 import org.mmbase.util.logging.Logging;
17 import org.mmbase.util.logging.Logger;
18
19 /**
20  * Simple file cache system that can be used by any servlet
21  *
22  * @application cache [utility, implementation]
23  * @javadoc
24  * @move org.mmbase.cache.implementation
25  * @rename Cache
26  * @author $Author: nklasens $
27  * @version $Id: cache.java,v 1.13 2005/09/20 19:28:29 nklasens Exp $
28  */

29 public class cache extends Module implements cacheInterface {
30
31     // logging
32
private static Logger log = Logging.getLoggerInstance(cache.class.getName());
33
34     /**
35      * @javadoc
36      */

37     private int MaxLines=1000;
38     /**
39      * @javadoc
40      */

41     private int MaxSize=100*1024;
42     /**
43      * @javadoc
44      */

45     private boolean active=true;
46     /**
47      * @javadoc
48      * @scope private
49      */

50     boolean state_up = false;
51     /**
52      * @javadoc
53      * @scope private
54      */

55     int hits,miss;
56     /**
57      * @javadoc
58      * @scope private
59      */

60     LRUHashtable lines = new LRUHashtable( MaxLines );
61
62     /**
63      * Simple file cache system that can be used by any servlet
64      */

65     public cache() {
66     }
67
68     /** @duplicate */
69     public void onload() {
70     }
71
72     /**
73      * @javadoc
74      */

75     public void reload() {
76         readParams();
77         if( MaxLines > 0 ) lines = new LRUHashtable( MaxLines );
78     }
79
80     /** @duplicate */
81     public void shutdown() {
82     }
83
84     /**
85      * Old interface to the inner table, will be removed soon
86      * @deprecated-now direct access to lines seems undesirable and is implementation-dependent
87      */

88     public LRUHashtable lines() {
89         return lines;
90     }
91
92     /**
93      * Try to get a cacheline from cache, returns null if not found
94      * @javadoc
95      */

96     public cacheline get(Object JavaDoc wanted) {
97         if (!active) return(null);
98         cacheline o=(cacheline)lines.get(wanted);
99         if (o==null) {
100             //if (log.isDebugEnabled()) log.debug("WOW CACHE MIS = "+wanted);
101
miss++;
102         } else {
103             //if (log.isDebugEnabled()) log.debug("WOW CACHE HIT = "+wanted);
104
hits++;
105         }
106         return o;
107     }
108
109     /**
110      * Try to put a cacheline in cache, returns old one if available
111      * In all other cases returns null.
112      * @javadoc
113      */

114     public cacheline put(Object JavaDoc key,Object JavaDoc value) {
115         if (!active) return(null);
116         // check if there is still room in the cache
117
// there is room so look at the cacheline and check size
118
cacheline line=(cacheline)value;
119         // if size is to big ignore the entry
120
if (line.filesize<MaxSize) {
121             // cacheline is oke place it in cache
122
return (cacheline)lines.put(key,value);
123         } else {
124             // cacheline to big
125
return null;
126         }
127     }
128
129     /**
130      * Clear the whole cache in one go
131      */

132     public boolean clear() {
133         lines.clear();
134         return false;
135     }
136
137     /**
138      * Remove the entry identified by key from the cache
139      * @javadoc
140      */

141     public cacheline remove(Object JavaDoc key) {
142         return (cacheline)lines.remove(key);
143     }
144
145     /**
146      * @javadoc
147      */

148     public void init() {
149         if (!state_up) {
150             state_up=true;
151         }
152         readParams();
153         if( MaxLines > 0 ) lines = new LRUHashtable( MaxLines );
154     }
155
156     /**
157      * @javadoc
158      */

159     public void unload() {
160     }
161
162     /**
163      * @javadoc
164      */

165     public Hashtable state() {
166         state.put("Hits",""+hits);
167         state.put("Misses",""+miss);
168         if (hits!=0 && miss!=0) {
169             state.put("Cache hits %",""+((hits+miss)*100)/hits);
170             state.put("Cache misses %",""+((hits+miss)*100)/miss);
171         }
172         state.put("Number cachelines",""+lines.size());
173         cacheline line;
174         int size=0;
175         for (Enumeration t=lines.elements();t.hasMoreElements();) {
176             line=(cacheline)t.nextElement();
177             size+=line.filesize;
178         }
179         state.put("Cache Size (in kb)",""+(size+1)/1024);
180         return state;
181     }
182
183
184     /**
185      * @javadoc
186      */

187     void readParams() {
188         String JavaDoc tmp = null;
189         try
190         {
191             tmp=getInitParameter("MaxLines");
192             if (tmp!=null) MaxLines=Integer.parseInt(tmp);
193             tmp=getInitParameter("MaxSize");
194             if (tmp!=null) MaxSize=Integer.parseInt(tmp)*1024;
195             tmp=getInitParameter("Active");
196         } catch (NumberFormatException JavaDoc e ) {
197             log.error("readParams(): " + e ) ;
198         }
199
200         if (tmp!=null && (tmp.equals("yes") || tmp.equals("Yes"))) {
201             active=true;
202         } else {
203             active=false;
204
205         }
206     }
207
208     /**
209      * @duplicate Should be handled with a standard method in Module that
210      * uses the configuration files.
211      */

212     public String JavaDoc getModuleInfo() {
213         return "this module provides cache function for http requests";
214     }
215 }
216
Popular Tags