1 10 package org.mmbase.cache; 11 12 import java.util.*; 13 14 import org.mmbase.util.*; 15 import org.mmbase.util.logging.Logger; 16 import org.mmbase.util.logging.Logging; 17 import org.mmbase.bridge.Cacheable; 18 19 25 abstract public class Cache implements SizeMeasurable, Map { 26 27 private static final Logger log = Logging.getLoggerInstance(Cache.class); 28 29 private boolean active = true; 30 protected int maxEntrySize = -1; 32 35 private CacheImplementationInterface implementation; 36 37 40 private int hits = 0; 41 42 45 private int misses = 0; 46 47 50 private int puts = 0; 51 52 public Cache(int size) { 53 implementation = new LRUHashtable(size); 54 log.service("Creating cache " + getName() + ": " + getDescription()); 55 } 56 57 void setImplementation(String clazz, Map configValues) { 58 try { 59 Class clas = Class.forName(clazz); 60 if (implementation == null || (! clas.equals(implementation.getClass()))) { 61 implementation = (CacheImplementationInterface) clas.newInstance(); 62 implementation.config(configValues); 63 } 64 } catch (ClassNotFoundException cnfe) { 65 log.error("For cache " + this + " " + cnfe.getClass().getName() + ": " + cnfe.getMessage()); 66 } catch (InstantiationException ie) { 67 log.error("For cache " + this + " " + ie.getClass().getName() + ": " + ie.getMessage()); 68 } catch (IllegalAccessException iae) { 69 log.error("For cache " + this + " " + iae.getClass().getName() + ": " + iae.getMessage()); 70 } 71 } 72 73 77 public String getName() { 78 return getClass().getName(); 79 } 80 81 85 public String getDescription() { 86 return "An all purpose Cache"; 87 } 88 89 90 91 96 public int getMaxEntrySize() { 97 if (getDefaultMaxEntrySize() > 0) { 98 return maxEntrySize; 99 } else { 100 return -1; 101 } 102 } 103 104 107 108 protected int getDefaultMaxEntrySize() { 109 return -1; 110 } 111 112 public Set entrySet() { 113 if (! active) return new HashSet(); 114 return implementation.entrySet(); 115 } 116 117 125 protected boolean checkCachePolicy(Object key) { 126 CachePolicy policy = null; 127 if (active) { 128 if (key instanceof Cacheable) { 129 policy = ((Cacheable)key).getCachePolicy(); 130 if (policy != null) { 131 return policy.checkPolicy(key); 132 } 133 } 134 return true; 135 } 136 return false; 137 } 138 139 142 public Object get(Object key) { 143 if (!checkCachePolicy(key)) { 144 return null; 145 } 146 Object res = implementation.get(key); 147 if (res != null) { 148 hits++; 149 } else { 150 misses++; 151 } 152 return res; 153 } 154 155 159 public Object put(Object key, Object value) { 160 if (!checkCachePolicy(key)) { 161 return null; 162 } 163 puts++; 164 return implementation.put(key, value); 165 } 166 167 171 public int getHits() { 172 return hits; 173 } 174 175 179 public int getMisses() { 180 return misses; 181 } 182 183 186 public int getPuts() { 187 return puts; 188 } 189 190 public void setMaxSize(int size) { 191 implementation.setMaxSize(size); 192 } 193 public int maxSize() { 194 return implementation.maxSize(); 195 } 196 197 200 public int size() { 201 return implementation.size(); 202 } 203 public boolean contains(Object key) { 204 return implementation.containsKey(key); 205 } 206 207 public int getCount(Object key) { 208 return implementation.getCount(key); 209 } 210 211 222 public double getRatio() { 223 return ((double) hits) / ( hits + misses ); 224 } 225 226 227 232 public String getStats() { 233 return "Access "+ (hits + misses) + " Ratio " + getRatio() + " Size " + size() + " Puts " + puts; 234 } 235 236 237 241 public void setActive(boolean a) { 242 active = a; 243 if (! active) { 244 implementation.clear(); 245 } 246 } 249 250 public String toString() { 251 return "Cache " + getName() + ", Ratio: " + getRatio() + " " + implementation; 252 } 253 254 257 public final boolean isActive() { 258 return active; 259 } 260 261 public int getByteSize() { 262 return getByteSize(new SizeOf()); 263 } 264 265 public int getByteSize(SizeOf sizeof) { 266 int size = 26; 267 if (implementation instanceof SizeMeasurable) { 268 size += ((SizeMeasurable) implementation).getByteSize(sizeof); 269 } else { 270 Iterator i = implementation.entrySet().iterator(); 272 while(i.hasNext()) { 273 Map.Entry entry = (Map.Entry) i.next(); 274 size += sizeof.sizeof(entry.getKey()); 275 size += sizeof.sizeof(entry.getValue()); 276 } 277 } 278 return size; 279 } 280 281 287 public int getCheapByteSize() { 288 int size = 0; 289 SizeOf sizeof = new SizeOf(); 290 Iterator i = implementation.entrySet().iterator(); 291 while(i.hasNext()) { 292 Map.Entry entry = (Map.Entry) i.next(); 293 size += sizeof.sizeof(entry.getKey()); 294 size += sizeof.sizeof(entry.getValue()); 295 sizeof.clear(); 296 } 297 return size; 298 } 299 300 301 304 public void clear() { 305 implementation.clear(); 306 } 307 308 309 312 public boolean containsKey(Object key) { 313 return implementation.containsKey(key); 314 } 315 316 317 320 public boolean containsValue(Object value) { 321 return implementation.containsValue(value); 322 } 323 324 325 328 public boolean equals(Object o) { 329 return implementation.equals(o); 331 } 332 333 334 337 public int hashCode() { 338 return implementation.hashCode(); 339 } 340 341 342 345 public boolean isEmpty() { 346 return implementation.isEmpty(); 347 } 348 349 350 353 public Set keySet() { 354 return implementation.keySet(); 355 } 356 357 358 361 public void putAll(Map t) { 362 implementation.putAll(t); 363 } 364 365 366 369 public Object remove(Object key) { 370 return implementation.remove(key); 371 } 372 373 374 377 public Collection values() { 378 return implementation.values(); 379 } 380 381 382 386 387 public Cache putCache() { 388 return CacheManager.putCache(this); 389 } 390 391 394 protected static Cache putCache(Cache cache) { 395 return CacheManager.putCache(cache); 396 } 397 398 401 public static Cache getCache(String name) { 402 return CacheManager.getCache(name); 403 } 404 405 408 public static Set getCaches() { 409 return CacheManager.getCaches(); 410 } 411 412 415 public static int getTotalByteSize() { 416 return CacheManager.getTotalByteSize(); 417 } 418 419 420 public static void main(String args[]) { 421 Cache mycache = new Cache(20000000) { 422 public String getName() { return "test cache"; } 423 public String getDescription() { return ""; } 424 }; 425 Runtime rt = Runtime.getRuntime(); 426 rt.gc(); 427 long usedBefore = rt.totalMemory() - rt.freeMemory(); 428 429 System.out.println("putting some strings in cache"); 430 mycache.put("aaa", "AAA"); mycache.put("bbb", "BBB"); 433 System.out.println("putting an hashmap in cache"); 434 Map m = new HashMap(); 435 m.put("ccc", "CCCCCC"); m.put("ddd", "DDD"); m.put("abc", "EEE"); mycache.put("eee", m); 440 441 for (int i = 1000000; i < 2000000; i++) { 445 mycache.put("a" + 1000000 + i, "b" + 1000000 + i); 446 } 450 rt.gc(); 451 452 long usedAfter = rt.totalMemory() - rt.freeMemory(); 453 454 System.out.println("1M of String costs " + (usedAfter - usedBefore) + " bytes"); 455 System.out.println("Sizeof reports " + SizeOf.getByteSize(mycache)); 456 System.out.println("size of cache: " + mycache.getByteSize() + " "); 457 458 } 459 460 } 461 | Popular Tags |