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:
     TimeInvalidatedCache.newBuilder()
         .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
        Parameters:
        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
        Returns:
        TimeInvalidatedCache fully built object
        Throws:
        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:
        TimeInvalidatedCacheBuilder
      • name

        public TimeInvalidatedCacheBuilder<K,​V> name​(String nameToSet)
        Sets the name of the cache, used for logging purposes, it is always required
        Parameters:
        nameToSet - Cache name
        Returns:
        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.
        Parameters:
        duration - Duration of time after which is considered expired
        Returns:
        this object
      • removalListener

        public TimeInvalidatedCacheBuilder<K,​V> removalListener​(BiConsumer<Map.Entry<K,​V>,​String> listener)
        Sets the removal listener which is notified every time a cache entry is invalidated.
        Parameters:
        listener - The removal listener. It is a BiConsumer with the invalidated entry as first argument and the removal cause as second argument.
        Returns:
        this object