View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import java.util.SortedMap;
4   
5   import com.atlassian.cache.CacheStatisticsKey;
6   import com.atlassian.util.concurrent.Supplier;
7   
8   import com.google.common.collect.ImmutableSortedMap;
9   
10  import net.sf.ehcache.statistics.StatisticsGateway;
11  
12  import static com.atlassian.cache.CacheStatisticsKey.EVICTION_COUNT;
13  import static com.atlassian.cache.CacheStatisticsKey.HEAP_SIZE;
14  import static com.atlassian.cache.CacheStatisticsKey.HIT_COUNT;
15  import static com.atlassian.cache.CacheStatisticsKey.MISS_COUNT;
16  import static com.atlassian.cache.CacheStatisticsKey.PUT_COUNT;
17  import static com.atlassian.cache.CacheStatisticsKey.REMOVE_COUNT;
18  import static com.atlassian.cache.CacheStatisticsKey.SIZE;
19  import static com.atlassian.util.concurrent.Suppliers.memoize;
20  
21  /**
22   * @since v2.4.0
23   */
24  public class DelegatingCacheStatistics
25  {
26      public static SortedMap<CacheStatisticsKey, Supplier<Long>> toStatistics(final StatisticsGateway stats)
27      {
28          // Note: While most of these statistics we can reasonably expect to be cheap and just memoize, the
29          // heap size in bytes is very likely to be expensive.  To make it possible to opt out of retrieving
30          // it, we hide it behind a Supplier, here.
31          final Supplier<Long> heapSize = new Supplier<Long>()
32          {
33              @Override
34              public Long get()
35              {
36                  return stats.getLocalHeapSizeInBytes();
37              }
38          };
39  
40          return ImmutableSortedMap.<CacheStatisticsKey,Supplier<Long>>orderedBy(CacheStatisticsKey.SORT_BY_LABEL)
41                  .put(SIZE, memoize(stats.getSize()))
42                  .put(HEAP_SIZE, heapSize)
43                  .put(HIT_COUNT, memoize(stats.cacheHitCount()))
44                  .put(PUT_COUNT, memoize(stats.cachePutCount()))
45                  .put(REMOVE_COUNT, memoize(stats.cacheRemoveCount()))
46                  .put(MISS_COUNT, memoize(stats.cacheMissCount()))
47                  .put(EVICTION_COUNT, memoize(stats.cacheEvictedCount()))
48                  .build();
49      }
50  }