KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.opensymphony.oscache.extra;
6
7 import com.opensymphony.oscache.base.events.CacheMapAccessEvent;
8 import com.opensymphony.oscache.base.events.CacheMapAccessEventListener;
9 import com.opensymphony.oscache.base.events.CacheMapAccessEventType;
10
11 /**
12  * Implementation of a CacheMapAccessEventListener. It uses the events to count
13  * the cache hit and misses.
14  * <p>
15  * We are not using any synchronized so that this does not become a bottleneck.
16  * The consequence is that on retrieving values, the operations that are
17  * currently being done won't be counted.
18  *
19  * @version $Revision: 1.1 $
20  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
21  * @author <a HREF="&#109;a&#105;&#108;&#116;&#111;:chris&#64;swebtec.&#99;&#111;&#109;">Chris Miller</a>
22  */

23 public class CacheMapAccessEventListenerImpl implements CacheMapAccessEventListener {
24     /**
25      * Hit counter
26      */

27     private int hitCount = 0;
28
29     /**
30      * Miss counter
31      */

32     private int missCount = 0;
33
34     /**
35      * Stale hit counter
36      */

37     private int staleHitCount = 0;
38
39     /**
40      * Constructor, empty for us
41      */

42     public CacheMapAccessEventListenerImpl() {
43     }
44
45     /**
46      * Returns the cache's current hit count
47      *
48      * @return The hit count
49      */

50     public int getHitCount() {
51         return hitCount;
52     }
53
54     /**
55      * Returns the cache's current miss count
56      *
57      * @return The miss count
58      */

59     public int getMissCount() {
60         return missCount;
61     }
62
63     /**
64      * Returns the cache's current stale hit count
65      */

66     public int getStaleHitCount() {
67         return staleHitCount;
68     }
69
70     /**
71      * This method handles an event each time the cache is accessed
72      *
73      * @param event The event triggered when the cache was accessed
74      */

75     public void accessed(CacheMapAccessEvent event) {
76         // Retrieve the event type and update the counters
77
CacheMapAccessEventType type = event.getEventType();
78
79         // Handles a hit event
80
if (type == CacheMapAccessEventType.HIT) {
81             hitCount++;
82         }
83         // Handles a stale hit event
84
else if (type == CacheMapAccessEventType.STALE_HIT) {
85             staleHitCount++;
86         }
87         // Handles a miss event
88
else if (type == CacheMapAccessEventType.MISS) {
89             missCount++;
90         } else {
91             // Unknown event!
92
throw new IllegalArgumentException JavaDoc("Unknown Cache Map Access event received");
93         }
94     }
95
96     /**
97      * Resets all of the totals to zero
98      */

99     public void reset() {
100         hitCount = 0;
101         staleHitCount = 0;
102         missCount = 0;
103     }
104
105     /**
106      * Return the counters in a string form
107      */

108     public String JavaDoc toString() {
109         return ("Hit count = " + hitCount + ", stale hit count = " + staleHitCount + " and miss count = " + missCount);
110     }
111 }
112
Popular Tags