Now that we've learned how to create a powerful caching mechanism, we are going to want to gather statistics on how our cache is performing and how it's being used. There is a very easy way to gather information on how our cache is performing. Keep in mind that tracking cache operations incurs a performance penalty. To gather statistics on our cache, we just need to specify that we want to record the statistics when using CacheBuilder
:
LoadingCache<String,TradeAccount> tradeAccountCache = CacheBuilder.newBuilder() .recordStats()
Here we are using a familiar pattern for constructing a LoadingCache
instance. To enable the recording of statistics, all we need to do is add a recordStats()
call on our builder. To read the performance statistics, all we need to do is call the stats()
method on our Cache
/LoadingCache
instance, and we will get a reference to a CacheStats
instance. Let's take the following example:
CacheStats cacheStats = cache.stats();
The following list...