1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.commons.jcs3.jcache.jmx;
20  
21  import javax.cache.management.CacheStatisticsMXBean;
22  
23  import org.apache.commons.jcs3.jcache.Statistics;
24  
25  public class JCSCacheStatisticsMXBean implements CacheStatisticsMXBean
26  {
27      private final Statistics statistics;
28  
29      public JCSCacheStatisticsMXBean(final Statistics stats)
30      {
31          this.statistics = stats;
32      }
33  
34      @Override
35      public void clear()
36      {
37          statistics.reset();
38      }
39  
40      @Override
41      public long getCacheHits()
42      {
43          return statistics.getHits();
44      }
45  
46      @Override
47      public float getCacheHitPercentage()
48      {
49          final long hits = getCacheHits();
50          if (hits == 0)
51          {
52              return 0;
53          }
54          return (float) hits / getCacheGets() * 100.0f;
55      }
56  
57      @Override
58      public long getCacheMisses()
59      {
60          return statistics.getMisses();
61      }
62  
63      @Override
64      public float getCacheMissPercentage()
65      {
66          final long misses = getCacheMisses();
67          if (misses == 0)
68          {
69              return 0;
70          }
71          return (float) misses / getCacheGets() * 100.0f;
72      }
73  
74      @Override
75      public long getCacheGets()
76      {
77          return getCacheHits() + getCacheMisses();
78      }
79  
80      @Override
81      public long getCachePuts()
82      {
83          return statistics.getPuts();
84      }
85  
86      @Override
87      public long getCacheRemovals()
88      {
89          return statistics.getRemovals();
90      }
91  
92      @Override
93      public long getCacheEvictions()
94      {
95          return statistics.getEvictions();
96      }
97  
98      @Override
99      public float getAverageGetTime()
100     {
101         return averageTime(statistics.getTimeTakenForGets());
102     }
103 
104     @Override
105     public float getAveragePutTime()
106     {
107         return averageTime(statistics.getTimeTakenForPuts());
108     }
109 
110     @Override
111     public float getAverageRemoveTime()
112     {
113         return averageTime(statistics.getTimeTakenForRemovals());
114     }
115 
116     private float averageTime(final long timeTaken)
117     {
118         final long gets = getCacheGets();
119         if (timeTaken == 0 || gets == 0)
120         {
121             return 0;
122         }
123         return timeTaken / gets;
124     }
125 }