1 package org.shiftone.cache.decorator.stat; 2 3 4 5 import org.shiftone.cache.Cache; 6 import org.shiftone.cache.util.Log; 7 8 import java.util.Date ; 9 10 11 17 public class StatCache implements Cache 18 { 19 20 private static final Log LOG = new Log(StatCache.class); 21 private final Cache cache; 22 private final String name; 23 private final Date createDate = new Date (); 24 private Sequence getCount = new Sequence(); 25 private Sequence addCount = new Sequence(); 26 private Sequence removeCount = new Sequence(); 27 private Sequence missCount = new Sequence(); 28 private Sequence hitCount = new Sequence(); 29 private int maxSize = 0; 30 31 public StatCache(String name, Cache cache) 32 { 33 this.name = name; 34 this.cache = cache; 35 } 36 37 38 private synchronized void updateMaxSize() { 39 int size = cache.size(); 40 if (size > maxSize) { 41 maxSize = size; 42 } 43 } 44 47 public void addObject(Object userKey, Object cacheObject) 48 { 49 addCount.increment(); 50 cache.addObject(userKey, cacheObject); 51 updateMaxSize(); 52 } 53 54 55 58 public Object getObject(Object key) 59 { 60 61 Object object = cache.getObject(key); 62 63 getCount.increment(); 64 65 if (object == null) 66 { 67 missCount.increment(); 68 } 69 else 70 { 71 hitCount.increment(); 72 } 73 74 return object; 75 } 76 77 78 81 public void remove(Object key) 82 { 83 removeCount.increment(); 84 cache.remove(key); 85 } 86 87 88 91 public int size() 92 { 93 return cache.size(); 94 } 95 96 97 100 public void clear() 101 { 102 cache.clear(); 103 } 104 105 106 110 public double getHitRatio() 111 { 112 113 long tryCount = hitCount.getValue() + missCount.getValue(); 114 115 return (tryCount == 0) 116 ? 0 117 : (hitCount.getValue() / tryCount); 118 } 119 120 121 124 public String toString() 125 { 126 return "StatCache->" + cache; 127 } 128 129 130 public void printStats() 131 { 132 133 LOG.info("Stats : " + name + " - " + cache + " - " + createDate); 134 LOG.info("\t:\tgetCount = " + getCount); 135 LOG.info("\t:\taddCount = " + addCount); 136 LOG.info("\t:\tremoveCount = " + removeCount); 137 LOG.info("\t:\tmissCount = " + missCount); 138 LOG.info("\t:\tgetCount = " + getCount); 139 LOG.info("\t:\thitCount = " + hitCount); 140 } 141 } 142 | Popular Tags |