Class StatisticsProvider


  • public final class StatisticsProvider
    extends Object
    Utility class for common statistics handling patterns. This class provides static methods for creating and managing cache statistics, eliminating duplication across cache implementations.
    • Method Detail

      • createBasicStats

        public static CacheStats createBasicStats​(AtomicLong hitCount,
                                                  AtomicLong missCount)
        Creates a basic CacheStats object from hit and miss counters. This pattern is used by many cache implementations.
        Parameters:
        hitCount - the hit counter
        missCount - the miss counter
        Returns:
        a populated CacheStats object
      • createComprehensiveStats

        public static CacheStats createComprehensiveStats​(AtomicLong hitCount,
                                                          AtomicLong missCount,
                                                          AtomicLong loadCount,
                                                          AtomicLong loadTime,
                                                          AtomicLong evictionCount)
        Creates a comprehensive CacheStats object with all counters.
        Parameters:
        hitCount - the hit counter
        missCount - the miss counter
        loadCount - the load counter
        loadTime - the total load time in nanoseconds
        evictionCount - the eviction counter
        Returns:
        a fully populated CacheStats object
      • calculateHitRatio

        public static double calculateHitRatio​(long hitCount,
                                               long missCount)
        Calculates the hit ratio from hit and miss counts.
        Parameters:
        hitCount - the number of hits
        missCount - the number of misses
        Returns:
        the hit ratio between 0.0 and 1.0
      • calculateMissRatio

        public static double calculateMissRatio​(long hitCount,
                                                long missCount)
        Calculates the miss ratio from hit and miss counts.
        Parameters:
        hitCount - the number of hits
        missCount - the number of misses
        Returns:
        the miss ratio between 0.0 and 1.0
      • calculateAverageLoadTime

        public static double calculateAverageLoadTime​(long totalLoadTime,
                                                      long loadCount)
        Calculates the average load time.
        Parameters:
        totalLoadTime - the total load time in nanoseconds
        loadCount - the number of loads
        Returns:
        the average load time in nanoseconds, or 0.0 if no loads
      • recordHit

        public static void recordHit​(AtomicLong hitCount,
                                     boolean statsEnabled)
        Records a cache hit in a thread-safe manner.
        Parameters:
        hitCount - the hit counter to increment
        statsEnabled - whether statistics recording is enabled
      • recordMiss

        public static void recordMiss​(AtomicLong missCount,
                                      boolean statsEnabled)
        Records a cache miss in a thread-safe manner.
        Parameters:
        missCount - the miss counter to increment
        statsEnabled - whether statistics recording is enabled
      • recordLoad

        public static void recordLoad​(AtomicLong loadCount,
                                      AtomicLong loadTime,
                                      long operationTimeNanos,
                                      boolean statsEnabled)
        Records a cache load operation.
        Parameters:
        loadCount - the load counter to increment
        loadTime - the total load time counter to update
        operationTimeNanos - the time this operation took in nanoseconds
        statsEnabled - whether statistics recording is enabled
      • recordEviction

        public static void recordEviction​(AtomicLong evictionCount,
                                          boolean statsEnabled)
        Records a cache eviction.
        Parameters:
        evictionCount - the eviction counter to increment
        statsEnabled - whether statistics recording is enabled
      • resetCounters

        public static void resetCounters​(AtomicLong... counters)
        Resets all statistics counters to zero.
        Parameters:
        counters - the counters to reset
      • formatStatsSummary

        public static String formatStatsSummary​(long hitCount,
                                                long missCount,
                                                long loadCount,
                                                long evictionCount)
        Creates a formatted statistics summary string.
        Parameters:
        hitCount - the number of hits
        missCount - the number of misses
        loadCount - the number of loads
        evictionCount - the number of evictions
        Returns:
        a formatted statistics summary
      • formatDetailedMetrics

        public static String formatDetailedMetrics​(long hitCount,
                                                   long missCount,
                                                   long loadCount,
                                                   long totalLoadTime,
                                                   long evictionCount,
                                                   long cacheSize)
        Creates a detailed performance metrics string.
        Parameters:
        hitCount - the number of hits
        missCount - the number of misses
        loadCount - the number of loads
        totalLoadTime - the total load time in nanoseconds
        evictionCount - the number of evictions
        cacheSize - the current cache size
        Returns:
        a detailed performance summary