KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > extra > StatisticListenerImpl


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 /**
14  * A simple implementation of a statistic reporter which uses the
15  * CacheMapAccessEventListener, CacheEntryEventListener and ScopeEventListener.
16  * It uses the events to count the cache hit and misses and of course the
17  * flushes.
18  * <p>
19  * We are not using any synchronized so that this does not become a bottleneck.
20  * The consequence is that on retrieving values, the operations that are
21  * currently being done won't be counted.
22  */

23 public class StatisticListenerImpl implements CacheMapAccessEventListener, CacheEntryEventListener, ScopeEventListener {
24
25     private static transient final Log log = LogFactory.getLog(StatisticListenerImpl.class);
26
27     /**
28      * Hit counter
29      */

30     private int hitCount = 0;
31
32     /**
33      * Miss counter
34      */

35     private int missCount = 0;
36
37     /**
38      * Stale hit counter
39      */

40     private int staleHitCount = 0;
41
42     /**
43      * Hit counter sum
44      */

45     private int hitCountSum = 0;
46
47     /**
48      * Miss counter sum
49      */

50     private int missCountSum = 0;
51
52     /**
53      * Stale hit counter
54      */

55     private int staleHitCountSum = 0;
56
57     /**
58      * Flush hit counter
59      */

60     private int flushCount = 0;
61
62     /**
63      * Constructor, empty for us
64      */

65     public StatisticListenerImpl() {
66         log.info("Creation of StatisticListenerImpl");
67     }
68
69     /**
70      * This method handles an event each time the cache is accessed
71      *
72      * @param event The event triggered when the cache was accessed
73      * @see com.opensymphony.oscache.base.events.CacheMapAccessEventListener#accessed(CacheMapAccessEvent)
74      */

75     public void accessed(CacheMapAccessEvent event) {
76         String JavaDoc result = "N/A";
77
78         // Retrieve the event type and update the counters
79
CacheMapAccessEventType type = event.getEventType();
80
81         // Handles a hit event
82
if (type == CacheMapAccessEventType.HIT) {
83             hitCount++;
84             result = "HIT";
85         }
86         // Handles a stale hit event
87
else if (type == CacheMapAccessEventType.STALE_HIT) {
88             staleHitCount++;
89             result = "STALE HIT";
90         }
91         // Handles a miss event
92
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     /**
105      * Logs the flush of the cache.
106      *
107      * @param info the string to be logged.
108      */

109     private void flushed(String JavaDoc 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     /**
129      * Event fired when a specific or all scopes are flushed.
130      *
131      * @param event ScopeEvent
132      * @see com.opensymphony.oscache.base.events.ScopeEventListener#scopeFlushed(ScopeEvent)
133      */

134     public void scopeFlushed(ScopeEvent event) {
135         flushed("scope " + ScopeEventListenerImpl.SCOPE_NAMES[event.getScope()]);
136     }
137
138     /**
139      * Event fired when an entry is added to the cache.
140      *
141      * @param event CacheEntryEvent
142      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryAdded(CacheEntryEvent)
143      */

144     public void cacheEntryAdded(CacheEntryEvent event) {
145         // do nothing
146
}
147
148     /**
149      * Event fired when an entry is flushed from the cache.
150      *
151      * @param event CacheEntryEvent
152      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryFlushed(CacheEntryEvent)
153      */

154     public void cacheEntryFlushed(CacheEntryEvent event) {
155         // do nothing, because a group or other flush is coming
156
if (!Cache.NESTED_EVENT.equals(event.getOrigin())) {
157             flushed("entry " + event.getKey() + " / " + event.getOrigin());
158         }
159     }
160
161     /**
162      * Event fired when an entry is removed from the cache.
163      *
164      * @param event CacheEntryEvent
165      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryRemoved(CacheEntryEvent)
166      */

167     public void cacheEntryRemoved(CacheEntryEvent event) {
168         // do nothing
169
}
170
171     /**
172      * Event fired when an entry is updated in the cache.
173      *
174      * @param event CacheEntryEvent
175      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheEntryUpdated(CacheEntryEvent)
176      */

177     public void cacheEntryUpdated(CacheEntryEvent event) {
178         // do nothing
179
}
180
181     /**
182      * Event fired when a group is flushed from the cache.
183      *
184      * @param event CacheGroupEvent
185      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheGroupFlushed(CacheGroupEvent)
186      */

187     public void cacheGroupFlushed(CacheGroupEvent event) {
188         flushed("group " + event.getGroup());
189     }
190
191     /**
192      * Event fired when a key pattern is flushed from the cache.
193      *
194      * @param event CachePatternEvent
195      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cachePatternFlushed(CachePatternEvent)
196      */

197     public void cachePatternFlushed(CachePatternEvent event) {
198         flushed("pattern " + event.getPattern());
199     }
200
201     /**
202      * An event that is fired when an entire cache gets flushed.
203      *
204      * @param event CachewideEvent
205      * @see com.opensymphony.oscache.base.events.CacheEntryEventListener#cacheFlushed(CachewideEvent)
206      */

207     public void cacheFlushed(CachewideEvent event) {
208         flushed("wide " + event.getDate());
209     }
210
211     /**
212      * Return the counters in a string form
213      *
214      * @return String
215      */

216     public String JavaDoc toString() {
217         return "StatisticListenerImpl: Hit = " + hitCount + " / " + hitCountSum
218                 + ", stale hit = " + staleHitCount + " / " + staleHitCountSum
219                 + ", miss = " + missCount + " / " + missCountSum + ", flush = "
220                 + flushCount;
221     }
222 }
Popular Tags