Class CaffeineCacheMetrics

java.lang.Object
io.micrometer.core.instrument.binder.cache.CacheMeterBinder
com.broadleafcommerce.common.extension.cache.caffeine.CaffeineCacheMetrics
All Implemented Interfaces:
io.micrometer.core.instrument.binder.MeterBinder

public class CaffeineCacheMetrics extends io.micrometer.core.instrument.binder.cache.CacheMeterBinder
A helper class for monitoring and exposing Caffeine cache metrics through Micrometer.

This implementation utilizes a background thread to refresh the cache statistics (e.g., hit/miss counts, estimated size, and eviction weight) upon a scrape request. By offloading these calculations—particularly the aggregation of concurrent LongAdder arrays in cache.stats() and the traversal of the eviction policy—we prevent blocking the main request threads (such as Tomcat HTTP threads) during Micrometer scrapes.

If a refresh is already in progress, additional scrape requests will use the currently available metrics and will not spawn additional tasks.

Additionally, if the cache is configured with a weigher that tracks byte sizes (such as with a CaffeineProxy), this binder exposes a cache.memory.used gauge to accurately report the total on-heap memory footprint of the cache region.

  • Constructor Details

    • CaffeineCacheMetrics

      public CaffeineCacheMetrics(ExecutorService refreshExecutor, com.github.benmanes.caffeine.cache.Cache<?,?> cache, String name, Iterable<io.micrometer.core.instrument.Tag> tags)
  • Method Details

    • monitor

      public static CaffeineCacheMetrics monitor(io.micrometer.core.instrument.MeterRegistry registry, ExecutorService refreshExecutor, com.github.benmanes.caffeine.cache.Cache<?,?> cache, String name, String... tags)
    • size

      protected Long size()
      Specified by:
      size in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder
    • hitCount

      protected long hitCount()
      Specified by:
      hitCount in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder
    • missCount

      protected Long missCount()
      Specified by:
      missCount in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder
    • evictionCount

      protected Long evictionCount()
      Specified by:
      evictionCount in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder
    • putCount

      protected long putCount()
      Specified by:
      putCount in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder
    • bindImplementationSpecificMetrics

      protected void bindImplementationSpecificMetrics(io.micrometer.core.instrument.MeterRegistry registry)
      Specified by:
      bindImplementationSpecificMetrics in class io.micrometer.core.instrument.binder.cache.CacheMeterBinder