Class CachedGauge<T>

  • Type Parameters:
    T - the type of the gauge's value
    All Implemented Interfaces:
    Gauge<T>, Metric

    public abstract class CachedGauge<T>
    extends java.lang.Object
    implements Gauge<T>
    A Gauge implementation which caches its value for a period of time.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Clock clock  
      private java.util.concurrent.atomic.AtomicLong reloadAt  
      private long timeoutNS  
      private T value  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CachedGauge​(long timeout, java.util.concurrent.TimeUnit timeoutUnit)
      Creates a new cached gauge with the given timeout period.
      protected CachedGauge​(Clock clock, long timeout, java.util.concurrent.TimeUnit timeoutUnit)
      Creates a new cached gauge with the given clock and timeout period.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T getValue()
      Returns the metric's current value.
      protected abstract T loadValue()
      Loads the value and returns it.
      private boolean shouldLoad()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • clock

        private final Clock clock
      • reloadAt

        private final java.util.concurrent.atomic.AtomicLong reloadAt
      • timeoutNS

        private final long timeoutNS
      • value

        private volatile T value
    • Constructor Detail

      • CachedGauge

        protected CachedGauge​(long timeout,
                              java.util.concurrent.TimeUnit timeoutUnit)
        Creates a new cached gauge with the given timeout period.
        Parameters:
        timeout - the timeout
        timeoutUnit - the unit of timeout
      • CachedGauge

        protected CachedGauge​(Clock clock,
                              long timeout,
                              java.util.concurrent.TimeUnit timeoutUnit)
        Creates a new cached gauge with the given clock and timeout period.
        Parameters:
        clock - the clock used to calculate the timeout
        timeout - the timeout
        timeoutUnit - the unit of timeout
    • Method Detail

      • loadValue

        protected abstract T loadValue()
        Loads the value and returns it.
        Returns:
        the new value
      • getValue

        public T getValue()
        Description copied from interface: Gauge
        Returns the metric's current value.
        Specified by:
        getValue in interface Gauge<T>
        Returns:
        the metric's current value
      • shouldLoad

        private boolean shouldLoad()