Class CacheEntry<T>

  • Type Parameters:
    T - The type of the object to store
    All Implemented Interfaces:
    Serializable, Supplier<Optional<T>>

    public final class CacheEntry<T>
    extends Object
    implements Serializable, Supplier<Optional<T>>
    Cache entry for use with the CacheMap class or other purposes.

    This class allows you to store a dated object of any type. The entry will be considered expired when the current date is after the expiration date.

    Instances of this class are Serializable if the contained type T is serializable.

    See Also:
    Serialized Form
    • Constructor Detail

      • CacheEntry

        public CacheEntry​(T entryValue,
                          long futureTimeAmount,
                          TimeUnit timeUnit)
        Construct a new cache entry that expires a specific amount of time in the future
        Parameters:
        entryValue - The entry value
        futureTimeAmount - The amount of time in the future (from right now) to expire it
        timeUnit - The units in which the amount of time is specified
      • CacheEntry

        public CacheEntry​(T entryValue,
                          Date entryExpiration)
        Construct a new cache entry that expires on a specific date
        Parameters:
        entryValue - The value of this cache entry
        entryExpiration - The expiration date of this cache entry
      • CacheEntry

        public CacheEntry​(T entryValue,
                          long entryExpirationTimestampMillis)
        Construct a new cache entry that expires at a specific Unix timestamp (in milliseconds)
        Parameters:
        entryValue - The value of this cache entry
        entryExpirationTimestampMillis - The expiration date of this cache entry
    • Method Detail

      • computeIfExpired

        public static <T> CacheEntry<T> computeIfExpired​(CacheEntry<T> entry,
                                                         Supplier<CacheEntry<T>> valueSupplier)
        Returns either the entry (if it is not null and not expired), or invokes the Supplier to generate a new entry if it is expired.
        Type Parameters:
        T - The type of thing that is cached
        Parameters:
        entry - The existing entry (which can be null)
        valueSupplier - A function to calculate a new value
        Returns:
        The value supplier
      • getExpiration

        public long getExpiration()
        Returns the expiration instant, in Unix timestamp millis
        Returns:
        The expiration timestamp
      • getValue

        public T getValue()
        Gets the value associated with this cache entry
        Returns:
        The cached value
      • isExpired

        public boolean isExpired()
        The entry is expired if the current time is after the expiration date
        Returns:
        True if expired