1 5 package com.opensymphony.oscache.extra; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import com.opensymphony.oscache.base.Cache; 11 import com.opensymphony.oscache.base.events.*; 12 13 23 public class StatisticListenerImpl implements CacheMapAccessEventListener, CacheEntryEventListener, ScopeEventListener { 24 25 private static transient final Log log = LogFactory.getLog(StatisticListenerImpl.class); 26 27 30 private int hitCount = 0; 31 32 35 private int missCount = 0; 36 37 40 private int staleHitCount = 0; 41 42 45 private int hitCountSum = 0; 46 47 50 private int missCountSum = 0; 51 52 55 private int staleHitCountSum = 0; 56 57 60 private int flushCount = 0; 61 62 65 public StatisticListenerImpl() { 66 log.info("Creation of StatisticListenerImpl"); 67 } 68 69 75 public void accessed(CacheMapAccessEvent event) { 76 String result = "N/A"; 77 78 CacheMapAccessEventType type = event.getEventType(); 80 81 if (type == CacheMapAccessEventType.HIT) { 83 hitCount++; 84 result = "HIT"; 85 } 86 else if (type == CacheMapAccessEventType.STALE_HIT) { 88 staleHitCount++; 89 result = "STALE HIT"; 90 } 91 else if (type == CacheMapAccessEventType.MISS) { 93 missCount++; 94 result = "MISS"; 95 } 96 97 if (log.isDebugEnabled()) { 98 log.debug("ACCESS : " + result + ": " + event.getCacheEntryKey()); 99 log.debug("STATISTIC : Hit = " + hitCount + ", stale hit =" 100 + staleHitCount + ", miss = " + missCount); 101 } 102 } 103 104 109 private void flushed(String info) { 110 flushCount++; 111 112 hitCountSum += hitCount; 113 staleHitCountSum += staleHitCount; 114 missCountSum += missCount; 115 116 if (log.isInfoEnabled()) { 117 log.info("FLUSH : " + info); 118 log.info("STATISTIC SUM : " + "Hit = " + hitCountSum 119 + ", stale hit = " + staleHitCountSum + ", miss = " 120 + missCountSum + ", flush = " + flushCount); 121 } 122 123 hitCount = 0; 124 staleHitCount = 0; 125 missCount = 0; 126 } 127 128 134 public void scopeFlushed(ScopeEvent event) { 135 flushed("scope " + ScopeEventListenerImpl.SCOPE_NAMES[event.getScope()]); 136 } 137 138 144 public void cacheEntryAdded(CacheEntryEvent event) { 145 } 147 148 154 public void cacheEntryFlushed(CacheEntryEvent event) { 155 if (!Cache.NESTED_EVENT.equals(event.getOrigin())) { 157 flushed("entry " + event.getKey() + " / " + event.getOrigin()); 158 } 159 } 160 161 167 public void cacheEntryRemoved(CacheEntryEvent event) { 168 } 170 171 177 public void cacheEntryUpdated(CacheEntryEvent event) { 178 } 180 181 187 public void cacheGroupFlushed(CacheGroupEvent event) { 188 flushed("group " + event.getGroup()); 189 } 190 191 197 public void cachePatternFlushed(CachePatternEvent event) { 198 flushed("pattern " + event.getPattern()); 199 } 200 201 207 public void cacheFlushed(CachewideEvent event) { 208 flushed("wide " + event.getDate()); 209 } 210 211 216 public String toString() { 217 return "StatisticListenerImpl: Hit = " + hitCount + " / " + hitCountSum 218 + ", stale hit = " + staleHitCount + " / " + staleHitCountSum 219 + ", miss = " + missCount + " / " + missCountSum + ", flush = " 220 + flushCount; 221 } 222 } | Popular Tags |