1 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 29 public class cache extends Module implements cacheInterface { 30 31 private static Logger log = Logging.getLoggerInstance(cache.class.getName()); 33 34 37 private int MaxLines=1000; 38 41 private int MaxSize=100*1024; 42 45 private boolean active=true; 46 50 boolean state_up = false; 51 55 int hits,miss; 56 60 LRUHashtable lines = new LRUHashtable( MaxLines ); 61 62 65 public cache() { 66 } 67 68 69 public void onload() { 70 } 71 72 75 public void reload() { 76 readParams(); 77 if( MaxLines > 0 ) lines = new LRUHashtable( MaxLines ); 78 } 79 80 81 public void shutdown() { 82 } 83 84 88 public LRUHashtable lines() { 89 return lines; 90 } 91 92 96 public cacheline get(Object wanted) { 97 if (!active) return(null); 98 cacheline o=(cacheline)lines.get(wanted); 99 if (o==null) { 100 miss++; 102 } else { 103 hits++; 105 } 106 return o; 107 } 108 109 114 public cacheline put(Object key,Object value) { 115 if (!active) return(null); 116 cacheline line=(cacheline)value; 119 if (line.filesize<MaxSize) { 121 return (cacheline)lines.put(key,value); 123 } else { 124 return null; 126 } 127 } 128 129 132 public boolean clear() { 133 lines.clear(); 134 return false; 135 } 136 137 141 public cacheline remove(Object key) { 142 return (cacheline)lines.remove(key); 143 } 144 145 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 159 public void unload() { 160 } 161 162 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 187 void readParams() { 188 String 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 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 212 public String getModuleInfo() { 213 return "this module provides cache function for http requests"; 214 } 215 } 216 | Popular Tags |