View Javadoc
1   package com.atlassian.cache.memory.jmx;
2   
3   import com.atlassian.cache.CacheManager;
4   import com.atlassian.cache.CacheStatisticsKey;
5   
6   import javax.annotation.Nonnull;
7   
8   /**
9    * JMX MBean implementation for Guava Cache
10   *
11   * @since v3.1
12   */
13  public class MemoryCacheMXBeanImpl implements MemoryCacheMXBean
14  {
15      
16      private CacheManager cacheManager;
17      private String cacheName;
18  
19  
20      public MemoryCacheMXBeanImpl(@Nonnull CacheManager cacheManager, @Nonnull String cacheName)
21      {
22          this.cacheManager = cacheManager;
23          this.cacheName = cacheName;
24      }
25  
26      @Override
27      public long getRequestCount()
28      {
29          return getProperty(CacheStatisticsKey.REQUEST_COUNT, 0L);
30      }
31  
32      @Override
33      public long getHitCount()
34      {
35          return getProperty(CacheStatisticsKey.HIT_COUNT, 0L);
36      }
37  
38      @Override
39      public double getHitRate()
40      {
41          long requestCount = getRequestCount();
42          return (requestCount == 0) ? 1.0 : (double) getHitCount() / requestCount;
43      }
44  
45      @Override
46      public long getMissCount()
47      {
48          return getProperty(CacheStatisticsKey.MISS_COUNT, 0L);
49      }
50  
51      @Override
52      public double getMissRate()
53      {
54          long requestCount = getRequestCount();
55          return (requestCount == 0) ? 0.0 : (double) getMissCount() / requestCount;
56      }
57  
58      @Override
59      public long getLoadCount()
60      {
61          return getProperty(CacheStatisticsKey.LOAD_COUNT, 0L);
62      }
63  
64      @Override
65      public long getLoadSuccessCount()
66      {
67          return getProperty(CacheStatisticsKey.LOAD_SUCCESS_COUNT, 0L);
68      }
69  
70      @Override
71      public long getLoadExceptionCount()
72      {
73          return getProperty(CacheStatisticsKey.LOAD_EXCEPTION_COUNT, 0L);
74      }
75  
76      @Override
77      public double getLoadExceptionRate()
78      {
79          long loadSuccessCount = getLoadSuccessCount();
80          long loadExceptionCount = getLoadExceptionCount();
81          long totalLoadCount = loadSuccessCount + loadExceptionCount;
82          return (totalLoadCount == 0) ? 0.0 : (double) loadExceptionCount / totalLoadCount;
83      }
84  
85      @Override
86      public long getTotalLoadTime()
87      {
88          return getProperty(CacheStatisticsKey.TOTAL_LOAD_TIME, 0L);
89      }
90  
91      @Override
92      public double getAverageLoadPenalty()
93      {
94          long totalLoadCount = getLoadSuccessCount() + getLoadExceptionCount();
95          return (totalLoadCount == 0) ? 0.0 : (double) getTotalLoadTime() / totalLoadCount;
96      }
97  
98      @Override
99      public long getEvictionCount()
100     {
101         return getProperty(CacheStatisticsKey.EVICTION_COUNT, 0L);
102     }
103 
104     @Override
105     public long getSize()
106     {
107         return getProperty(CacheStatisticsKey.SIZE, 0L);
108     }
109 
110     private long getProperty(CacheStatisticsKey propertyName, long defaultValue)
111     {
112         if(cacheManager.getManagedCache(cacheName) == null
113                 || cacheManager.getManagedCache(cacheName).getStatistics().get(propertyName) == null)
114         {
115             return defaultValue;
116         }
117         return cacheManager.getManagedCache(cacheName).getStatistics().get(propertyName).get();
118     }
119 }