Interface TimeReference


  • 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.

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

    Author:
    Jens Wilke
    • Method Detail

      • millis

        long millis()
        Returns the milliseconds since epoch. In the simulated clock a call to this method would make time pass in small increments.

        It is possible to use other time scales and references (e.g. nano seconds). In this case the method toMillis(long) needs to be implemented. All times in the cache API, e.g. MutableCacheEntry.getExpiryTime() or in AdvancedCacheLoader.load(Object, long, CacheEntry) be based on the time defined here. That may lead to confusion and should be used with caution.

      • sleep

        void sleep​(long millis)
            throws InterruptedException
        Wait for the specified amount of time in milliseconds.

        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
      • toMillis

        default long toMillis​(long millis)
        Convert a value returned by millis() to milliseconds since epoch. This can be overridden in case another time scale and or reference is used. Conversion is needed for correctly scheduling timer task that regularly process the expiry tasks.