View Javadoc
1   package com.atlassian.cache.memory;
2   
3   import com.atlassian.cache.CacheStatisticsKey;
4   import com.atlassian.instrumentation.caches.CacheCollector;
5   import java.util.function.Supplier;
6   import com.google.common.cache.Cache;
7   import com.google.common.cache.LoadingCache;
8   import com.google.common.collect.ImmutableSortedMap;
9   
10  import java.util.SortedMap;
11  
12  import static com.atlassian.cache.CacheStatisticsKey.EVICTION_COUNT;
13  import static com.atlassian.cache.CacheStatisticsKey.HIT_COUNT;
14  import static com.atlassian.cache.CacheStatisticsKey.LOAD_COUNT;
15  import static com.atlassian.cache.CacheStatisticsKey.LOAD_EXCEPTION_COUNT;
16  import static com.atlassian.cache.CacheStatisticsKey.LOAD_SUCCESS_COUNT;
17  import static com.atlassian.cache.CacheStatisticsKey.MISS_COUNT;
18  import static com.atlassian.cache.CacheStatisticsKey.PUT_COUNT;
19  import static com.atlassian.cache.CacheStatisticsKey.REMOVE_COUNT;
20  import static com.atlassian.cache.CacheStatisticsKey.REQUEST_COUNT;
21  import static com.atlassian.cache.CacheStatisticsKey.SIZE;
22  import static com.atlassian.cache.CacheStatisticsKey.TOTAL_LOAD_TIME;
23  import static com.atlassian.cache.CacheStatisticsKey.TOTAL_MISS_TIME;
24  
25  /**
26   * @since v2.4.0
27   */
28  public class DelegatingCacheStatistics
29  {
30      public static SortedMap<CacheStatisticsKey, Supplier<Long>> toStatistics(Cache<?, ?> internalCache)
31      {
32          ImmutableSortedMap.Builder<CacheStatisticsKey, Supplier<Long>> map = ImmutableSortedMap.<CacheStatisticsKey, Supplier<Long>>orderedBy(CacheStatisticsKey.SORT_BY_LABEL)
33                  .put(SIZE, memoize(internalCache.size()))
34                  .put(HIT_COUNT, memoize(internalCache.stats().hitCount()))
35                  .put(MISS_COUNT, memoize(internalCache.stats().missCount()))
36                  .put(LOAD_COUNT, memoize(internalCache.stats().loadCount()))
37                  .put(LOAD_SUCCESS_COUNT, memoize(internalCache.stats().loadSuccessCount()))
38                  .put(LOAD_EXCEPTION_COUNT, memoize(internalCache.stats().loadExceptionCount()))
39                  .put(EVICTION_COUNT, memoize(internalCache.stats().evictionCount()))
40                  .put(TOTAL_LOAD_TIME, memoize(internalCache.stats().totalLoadTime()))
41                  .put(REQUEST_COUNT, memoize(internalCache.stats().requestCount()));
42  
43          // don't bother reporting this statistic as "zero" for caches that
44          // can't even calculate it. Only LoadingCaches calculate it.
45          if (internalCache instanceof LoadingCache)
46          {
47              map.put(TOTAL_MISS_TIME, memoize(internalCache.stats().totalLoadTime()));
48          }
49  
50          return map.build();
51      }
52  
53      public static SortedMap<CacheStatisticsKey, Supplier<Long>> toStatistics(CacheCollector counter)
54      {
55          ImmutableSortedMap.Builder<CacheStatisticsKey, Supplier<Long>> map =
56                  ImmutableSortedMap.<CacheStatisticsKey, Supplier<Long>>orderedBy(CacheStatisticsKey.SORT_BY_LABEL)
57                          .put(HIT_COUNT, memoize(counter.getHits()))
58                          .put(MISS_COUNT, memoize(counter.getMisses()))
59                          .put(TOTAL_MISS_TIME, memoize(counter.getMissTime()))
60                          .put(PUT_COUNT, memoize(counter.getPuts()))
61                          .put(SIZE, memoize(counter.getCacheSize()))
62                          .put(REMOVE_COUNT, memoize(counter.getRemoves()));
63  
64          return map.build();
65      }
66  
67      private static <V> Supplier<V> memoize(final V value)
68      {
69          return new ImmediateSupplier<V>(value);
70      }
71  
72      static class ImmediateSupplier<V> implements Supplier<V>
73      {
74          private final V value;
75  
76          ImmediateSupplier(final V value)
77          {
78              this.value = value;
79          }
80  
81          @Override
82          public V get()
83          {
84              return value;
85          }
86  
87          @Override
88          public String toString()
89          {
90              return "ImmediateSupplier[" + value + ']';
91          }
92      }
93  }