KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > decorator > stat > StatCache


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 JavaDoc;
9
10
11 /**
12  * Class StatCache
13  *
14  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
15  * @version $Revision: 1.8 $
16  */

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 JavaDoc name;
23     private final Date JavaDoc createDate = new Date JavaDoc();
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 JavaDoc 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     /**
45      * Method addObject
46      */

47     public void addObject(Object JavaDoc userKey, Object JavaDoc cacheObject)
48     {
49         addCount.increment();
50         cache.addObject(userKey, cacheObject);
51         updateMaxSize();
52     }
53
54
55     /**
56      * Method getObject
57      */

58     public Object JavaDoc getObject(Object JavaDoc key)
59     {
60
61         Object JavaDoc 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     /**
79      * Method remove
80      */

81     public void remove(Object JavaDoc key)
82     {
83         removeCount.increment();
84         cache.remove(key);
85     }
86
87
88     /**
89      * Method size
90      */

91     public int size()
92     {
93         return cache.size();
94     }
95
96
97     /**
98      * Method clear
99      */

100     public void clear()
101     {
102         cache.clear();
103     }
104
105
106     /**
107      * Method getHitRatio is the ratio of hits to tries.
108      * hitCount / (hitCount + missCount)
109      */

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     /**
122      * Method toString
123      */

124     public String JavaDoc 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