Class PerSecondCache

java.lang.Object
io.micronaut.http.server.util.PerSecondCache

@Internal public final class PerSecondCache extends Object
Caches a string that is a pure function of the wall clock truncated to whole seconds, so that callers on many threads that need the same value within the same second only compute it once.

Thread safety: the cache is a single atomic reference to an immutable (second, text) pair. A reader loads the reference once and either returns the text, if the pair's second matches its own clock reading, or computes the text for its own second and publishes a new pair. Concurrent readers that both miss compute the same text for the same second and publish equivalent pairs; a reader that publishes a stale pair after a newer one only causes the next reader to recompute. In every interleaving a reader returns the text for the second it observed on the clock, never a torn or mixed value.

Since:
5.2.0
  • Constructor Details

    • PerSecondCache

      public PerSecondCache(LongFunction<String> formatter)
      Create a new cache.
      Parameters:
      formatter - Computes the text for a given epoch second
  • Method Details

    • now

      public String now()
      The text for the current wall-clock time.
      Returns:
      The text for the current second
    • get

      public String get(long epochMillis)
      The text for the given instant.
      Parameters:
      epochMillis - The instant, in milliseconds since the epoch
      Returns:
      The text for the second that contains the instant