Class AuthCache<K,V>

java.lang.Object
org.apache.cassandra.auth.AuthCache<K,V>
All Implemented Interfaces:
AuthCacheMBean, Shutdownable
Direct Known Subclasses:
AuthorizationProxy.JmxPermissionsCache, CIDRPermissionsCache, NetworkPermissionsCache, PasswordAuthenticator.CredentialsCache, PermissionsCache, RolesCache

public class AuthCache<K,V> extends Object implements AuthCacheMBean, Shutdownable
  • Field Details

    • MBEAN_NAME_BASE

      public static final String MBEAN_NAME_BASE
      See Also:
    • 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 Details

    • AuthCache

      protected AuthCache(String name, IntConsumer setValidityDelegate, IntSupplier getValidityDelegate, IntConsumer setUpdateIntervalDelegate, IntSupplier getUpdateIntervalDelegate, IntConsumer setMaxEntriesDelegate, IntSupplier getMaxEntriesDelegate, Consumer<Boolean> setActiveUpdate, BooleanSupplier getActiveUpdate, Function<K,V> loadFunction, Supplier<Map<K,V>> bulkLoadFunction, 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.
      setActiveUpdate - Method to process config to actively update the auth cache prior to configured cache expiration
      getActiveUpdate - Getter for active update
      loadFunction - Function to load the cache. Called on get(Object)
      cacheEnabledDelegate - Used to determine if cache is enabled.
    • AuthCache

      protected AuthCache(String name, IntConsumer setValidityDelegate, IntSupplier getValidityDelegate, IntConsumer setUpdateIntervalDelegate, IntSupplier getUpdateIntervalDelegate, IntConsumer setMaxEntriesDelegate, IntSupplier getMaxEntriesDelegate, Consumer<Boolean> setActiveUpdate, BooleanSupplier getActiveUpdate, Function<K,V> loadFunction, Supplier<Map<K,V>> bulkLoadFunction, BooleanSupplier cacheEnabledDelegate, BiPredicate<K,V> invalidationCondition)
      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.
      setActiveUpdate - Actively update the cache before expiry
      getActiveUpdate - Getter for active update
      loadFunction - Function to load the cache. Called on get(Object)
      cacheEnabledDelegate - Used to determine if cache is enabled.
      invalidationCondition - Used during active updates to determine if a refreshed value indicates a missing entry in the underlying table. If satisfied, the key will be invalidated.
  • Method Details

    • shutdownAllAndWait

      public static void shutdownAllAndWait(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
      Throws:
      InterruptedException
      TimeoutException
    • init

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

      protected void unregisterMBean()
    • getObjectName

      protected String getObjectName()
    • getAll

      public Map<K,V> getAll()
      Retrieve all cached entries. Will call Cache.asMap() which does not trigger "load".
      Returns:
      a map of cached key-value pairs
    • 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
    • getValidity

      public int getValidity()
      Specified by:
      getValidity in interface AuthCacheMBean
    • 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
    • getUpdateInterval

      public int getUpdateInterval()
      Specified by:
      getUpdateInterval in interface AuthCacheMBean
    • setMaxEntries

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

      public int getMaxEntries()
      Specified by:
      getMaxEntries in interface AuthCacheMBean
    • getActiveUpdate

      public boolean getActiveUpdate()
      Specified by:
      getActiveUpdate in interface AuthCacheMBean
    • setActiveUpdate

      public void setActiveUpdate(boolean update)
      Specified by:
      setActiveUpdate in interface AuthCacheMBean
    • getEstimatedSize

      public long getEstimatedSize()
      Specified by:
      getEstimatedSize in interface AuthCacheMBean
    • 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
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in interface Shutdownable
    • shutdown

      public void shutdown()
      Description copied from interface: Shutdownable
      Shutdown once any remaining work has completed (however this is defined for the implementation).
      Specified by:
      shutdown in interface Shutdownable
    • shutdownNow

      public Object shutdownNow()
      Description copied from interface: Shutdownable
      Shutdown immediately, possibly interrupting ongoing work, and cancelling work that is queued.
      Specified by:
      shutdownNow in interface Shutdownable
    • awaitTermination

      public boolean awaitTermination(long timeout, TimeUnit units) throws InterruptedException
      Description copied from interface: Shutdownable
      Await termination of this object, i.e. the cessation of all current and future work.
      Specified by:
      awaitTermination in interface Shutdownable
      Throws:
      InterruptedException
    • warm

      public void warm()