View Javadoc

1   package com.atlassian.cache.memory;
2   
3   import com.atlassian.cache.CacheStatisticsKey;
4   import com.atlassian.util.concurrent.Supplier;
5   import com.google.common.cache.Cache;
6   import com.google.common.cache.LoadingCache;
7   import com.google.common.collect.ImmutableSortedMap;
8   
9   import java.util.SortedMap;
10  
11  import static com.atlassian.cache.CacheStatisticsKey.EVICTION_COUNT;
12  import static com.atlassian.cache.CacheStatisticsKey.HIT_COUNT;
13  import static com.atlassian.cache.CacheStatisticsKey.MISS_COUNT;
14  import static com.atlassian.cache.CacheStatisticsKey.SIZE;
15  import static com.atlassian.cache.CacheStatisticsKey.TOTAL_MISS_TIME;
16  import static com.atlassian.util.concurrent.Suppliers.memoize;
17  
18  /**
19   * @since v2.4.0
20   */
21  public class DelegatingCacheStatistics
22  {
23      public static SortedMap<CacheStatisticsKey,Supplier<Long>> toStatistics(Cache<?, ?> internalCache)
24      {
25          ImmutableSortedMap.Builder<CacheStatisticsKey, Supplier<Long>> map = ImmutableSortedMap.<CacheStatisticsKey, Supplier<Long>>orderedBy(CacheStatisticsKey.SORT_BY_LABEL)
26                  .put(SIZE, memoize(internalCache.size()))
27                  .put(HIT_COUNT, memoize(internalCache.stats().hitCount()))
28                  .put(MISS_COUNT, memoize(internalCache.stats().missCount()))
29                  .put(EVICTION_COUNT, memoize(internalCache.stats().evictionCount()));
30  
31          // don't bother reporting this statistic as "zero" for caches that
32          // can't even calculate it. Only LoadingCaches calculate it.
33          if (internalCache instanceof LoadingCache)
34          {
35              map.put(TOTAL_MISS_TIME, memoize(internalCache.stats().totalLoadTime()));
36          }
37  
38          return map.build();
39      }
40  }