Class TimeInvalidatedCacheBuilder<K,​V>

  • Type Parameters:
    K - Key type used in cache
    V - Value type used in cache

    public class TimeInvalidatedCacheBuilder<K,​V>
    extends Object
    Allows building a TimeInvalidatedCache and initializing it. Do not instantiate directly, instead use TimeInvalidatedCache.newBuilder() method to create a new instance of this class. Expects to be called as follows:
         .name("CacheName") // Required name of the cache
         .expireAfterDuration(Duration.ofMinutes(5)) // Could be any Duration, not necessarily in
                                                     // minutes. If not executed, 1 minute default
                                                     // is assumed
         .build(key -> generateKeyValue(key))     // This is a lambda that initializes the key if it
      *                                           // expired or is the first time reading it. It is
      *                                           // required.
    • Method Detail

      • build

        public <K1 extends K,​V1 extends VTimeInvalidatedCache<K1,​V1> build​(Function<? super K1,​V1> loader)
        Builds the TimeInvalidatedCache
        loader - lambda that initializes the key if it expired or is the first time it is read. It should receive a key and return the value corresponding to it
        TimeInvalidatedCache fully built object
        OBException - If name or loader have not been set previous to executing the build function
        IllegalArgumentException - if duration is negative (previously executing expireDuration())
        IllegalStateException - if the time to live or variable expiration was already set (previously executing expireDuration())
        ArithmeticException - for durations greater than +/- approximately 292 year (previously executing expireDuration())
        See Also:
      • name

        public TimeInvalidatedCacheBuilder<K,​V> name​(String nameToSet)
        Sets the name of the cache, used for logging purposes, it is always required
        nameToSet - Cache name
        this object
      • expireAfterDuration

        public TimeInvalidatedCacheBuilder<K,​V> expireAfterDuration​(Duration duration)
        Sets the expiration duration, after this period the key is considered expired and reloaded on the next access. If not invoked, defaults to 1 minute.
        duration - Duration of time after which is considered expired
        this object