Class AuthCache<K,​V>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.github.benmanes.caffeine.cache.LoadingCache<K,​V> cache
      Underlying cache.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AuthCache​(java.lang.String name, java.util.function.IntConsumer setValidityDelegate, java.util.function.IntSupplier getValidityDelegate, java.util.function.IntConsumer setUpdateIntervalDelegate, java.util.function.IntSupplier getUpdateIntervalDelegate, java.util.function.IntConsumer setMaxEntriesDelegate, java.util.function.IntSupplier getMaxEntriesDelegate, java.util.function.Function<K,​V> loadFunction, java.util.function.BooleanSupplier cacheEnabledDelegate)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      V get​(K k)
      Retrieve a value from the cache.
      int getMaxEntries()  
      protected java.lang.String getObjectName()  
      int getUpdateInterval()  
      int getValidity()  
      protected void init()
      Do setup for the cache and MBean.
      protected com.github.benmanes.caffeine.cache.LoadingCache<K,​V> initCache​(com.github.benmanes.caffeine.cache.LoadingCache<K,​V> existing)
      (Re-)initialise the underlying cache.
      void invalidate()
      Invalidate the entire cache.
      void invalidate​(K k)
      Invalidate a key
      void setMaxEntries​(int maxEntries)
      Set maximum number of entries in the cache.
      void setUpdateInterval​(int updateInterval)
      Time in milliseconds after which an entry in the cache should be refreshed (it's load function called again)
      void setValidity​(int validityPeriod)
      Time in milliseconds that a value in the cache will expire after.
      protected void unregisterMBean()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cache

        protected volatile com.github.benmanes.caffeine.cache.LoadingCache<K,​V> cache
        Underlying cache. LoadingCache will call underlying load function on get(K) if key is not present
    • Constructor Detail

      • AuthCache

        protected AuthCache​(java.lang.String name,
                            java.util.function.IntConsumer setValidityDelegate,
                            java.util.function.IntSupplier getValidityDelegate,
                            java.util.function.IntConsumer setUpdateIntervalDelegate,
                            java.util.function.IntSupplier getUpdateIntervalDelegate,
                            java.util.function.IntConsumer setMaxEntriesDelegate,
                            java.util.function.IntSupplier getMaxEntriesDelegate,
                            java.util.function.Function<K,​V> loadFunction,
                            java.util.function.BooleanSupplier cacheEnabledDelegate)
        Parameters:
        name - Used for MBean
        setValidityDelegate - Used to set cache validity period. See Policy#expireAfterWrite()
        getValidityDelegate - Getter for validity period
        setUpdateIntervalDelegate - Used to set cache update interval. See Policy#refreshAfterWrite()
        getUpdateIntervalDelegate - Getter for update interval
        setMaxEntriesDelegate - Used to set max # entries in cache. See Policy.Eviction.setMaximum(long)
        getMaxEntriesDelegate - Getter for max entries.
        loadFunction - Function to load the cache. Called on get(Object)
        cacheEnabledDelegate - Used to determine if cache is enabled.
    • Method Detail

      • init

        protected void init()
        Do setup for the cache and MBean.
      • unregisterMBean

        protected void unregisterMBean()
      • getObjectName

        protected java.lang.String getObjectName()
      • get

        public V get​(K k)
        Retrieve a value from the cache. Will call LoadingCache.get(Object) which will "load" the value if it's not present, thus populating the key.
        Parameters:
        k -
        Returns:
        The current value of K if cached or loaded. See LoadingCache.get(Object) for possible exceptions.
      • invalidate

        public void invalidate()
        Invalidate the entire cache.
        Specified by:
        invalidate in interface AuthCacheMBean
      • invalidate

        public void invalidate​(K k)
        Invalidate a key
        Parameters:
        k - key to invalidate
      • setValidity

        public void setValidity​(int validityPeriod)
        Time in milliseconds that a value in the cache will expire after.
        Specified by:
        setValidity in interface AuthCacheMBean
        Parameters:
        validityPeriod - in milliseconds
      • setUpdateInterval

        public void setUpdateInterval​(int updateInterval)
        Time in milliseconds after which an entry in the cache should be refreshed (it's load function called again)
        Specified by:
        setUpdateInterval in interface AuthCacheMBean
        Parameters:
        updateInterval - in milliseconds
      • setMaxEntries

        public void setMaxEntries​(int maxEntries)
        Set maximum number of entries in the cache.
        Specified by:
        setMaxEntries in interface AuthCacheMBean
        Parameters:
        maxEntries -
      • initCache

        protected com.github.benmanes.caffeine.cache.LoadingCache<K,​V> initCache​(com.github.benmanes.caffeine.cache.LoadingCache<K,​V> existing)
        (Re-)initialise the underlying cache. Will update validity, max entries, and update interval if any have changed. The underlying LoadingCache will be initiated based on the provided loadFunction. Note: If you need some unhandled cache setting to be set you should extend AuthCache and override this method.
        Parameters:
        existing - If not null will only update cache update validity, max entries, and update interval.
        Returns:
        New LoadingCache if existing was null, otherwise the existing cache