Interface TimeReference

  • All Known Implementing Classes:
    TimeReference.Default, TimeReference.Milliseconds

    public interface TimeReference
    Time reference for a cache. By default, the current time is retrieved with System.currentTimeMillis(). Another time reference can be specified if the application uses a different time source or when a simulated clock should be used.

    For efficiency reasons cache2k always uses a long to store a time value internally. The resolution is expected to be milliseconds or higher. There is currently no intensive testing carried out with different timer resolutions.

    An instance may implement AutoCloseable if resources need to be cleaned up.

    Author:
    Jens Wilke
    • Field Detail

      • MINIMUM_TICKS

        static final long MINIMUM_TICKS
        Every time reference is expected to produce times higher or equal to this value. cache2k uses lower values for expired or invalid entries, which must be guaranteed in the past.
        See Also:
        Constant Field Values
    • Method Detail

      • ticks

        long ticks()
        Returns the timer ticks since a reference point, typically milliseconds since epoch. Expected to be always equal or higher than 100L.
      • sleep

        void sleep​(long ticks)
            throws InterruptedException
        Wait for the specified amount of time. This is only executed by tests and never by the cache itself.

        The value of 0 means that the thread should pause and other processing should be done. In a simulated clock this would wait for concurrent processing and, if no processing is happening, advance the time to the next event.

        Throws:
        InterruptedException
      • ticksToMillisCeiling

        long ticksToMillisCeiling​(long ticks)
        Convert a duration in ticks to milliseconds, rounding up to the next full millisecond.

        This can be overridden in case another timescale is used. Conversion is needed for correctly scheduling a timer tasks.

      • toTicks

        long toTicks​(Duration v)
        Convert a duration to ticks. Used to convert configuration durations.
      • ticksToInstant

        Instant ticksToInstant​(long timeInTicks)
        Convert a time value in ticks to an instant object. Needed for display purposes.