public class AuthCache<K,V> extends java.lang.Object implements AuthCacheMBean, Shutdownable
Modifier and Type | Class and Description |
---|---|
static interface |
AuthCache.BulkLoader<K,V> |
Modifier and Type | Field and Description |
---|---|
protected com.github.benmanes.caffeine.cache.LoadingCache<K,V> |
cache
Underlying cache.
|
static java.lang.String |
MBEAN_NAME_BASE |
Modifier | Constructor and 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.Consumer<java.lang.Boolean> setActiveUpdate,
java.util.function.BooleanSupplier getActiveUpdate,
java.util.function.Function<K,V> loadFunction,
java.util.function.Supplier<java.util.Map<K,V>> bulkLoadFunction,
java.util.function.BooleanSupplier cacheEnabledDelegate) |
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.Consumer<java.lang.Boolean> setActiveUpdate,
java.util.function.BooleanSupplier getActiveUpdate,
java.util.function.Function<K,V> loadFunction,
java.util.function.Supplier<java.util.Map<K,V>> bulkLoadFunction,
java.util.function.BooleanSupplier cacheEnabledDelegate,
java.util.function.BiPredicate<K,V> invalidationCondition) |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
java.util.concurrent.TimeUnit units)
Await termination of this object, i.e.
|
V |
get(K k)
Retrieve a value from the cache.
|
boolean |
getActiveUpdate() |
java.util.Map<K,V> |
getAll()
Retrieve all cached entries.
|
long |
getEstimatedSize() |
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.
|
boolean |
isTerminated() |
void |
setActiveUpdate(boolean update) |
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.
|
void |
shutdown()
Shutdown once any remaining work has completed (however this is defined for the implementation).
|
static void |
shutdownAllAndWait(long timeout,
java.util.concurrent.TimeUnit unit) |
java.lang.Object |
shutdownNow()
Shutdown immediately, possibly interrupting ongoing work, and cancelling work that is queued.
|
protected void |
unregisterMBean() |
void |
warm() |
public static final java.lang.String MBEAN_NAME_BASE
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.Consumer<java.lang.Boolean> setActiveUpdate, java.util.function.BooleanSupplier getActiveUpdate, java.util.function.Function<K,V> loadFunction, java.util.function.Supplier<java.util.Map<K,V>> bulkLoadFunction, java.util.function.BooleanSupplier cacheEnabledDelegate)
name
- Used for MBeansetValidityDelegate
- Used to set cache validity period. See Policy.expireAfterWrite()
getValidityDelegate
- Getter for validity periodsetUpdateIntervalDelegate
- Used to set cache update interval. See Policy.refreshAfterWrite()
getUpdateIntervalDelegate
- Getter for update intervalsetMaxEntriesDelegate
- 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 expirationgetActiveUpdate
- Getter for active updateloadFunction
- Function to load the cache. Called on get(Object)
cacheEnabledDelegate
- Used to determine if cache is enabled.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.Consumer<java.lang.Boolean> setActiveUpdate, java.util.function.BooleanSupplier getActiveUpdate, java.util.function.Function<K,V> loadFunction, java.util.function.Supplier<java.util.Map<K,V>> bulkLoadFunction, java.util.function.BooleanSupplier cacheEnabledDelegate, java.util.function.BiPredicate<K,V> invalidationCondition)
name
- Used for MBeansetValidityDelegate
- Used to set cache validity period. See Policy.expireAfterWrite()
getValidityDelegate
- Getter for validity periodsetUpdateIntervalDelegate
- Used to set cache update interval. See Policy.refreshAfterWrite()
getUpdateIntervalDelegate
- Getter for update intervalsetMaxEntriesDelegate
- Used to set max # entries in cache. See Policy.Eviction.setMaximum(long)
getMaxEntriesDelegate
- Getter for max entries.setActiveUpdate
- Actively update the cache before expirygetActiveUpdate
- Getter for active updateloadFunction
- 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.public static void shutdownAllAndWait(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
java.lang.InterruptedException
java.util.concurrent.TimeoutException
protected void init()
protected void unregisterMBean()
protected java.lang.String getObjectName()
public java.util.Map<K,V> getAll()
Cache.asMap()
which does not trigger "load".public V get(K k)
LoadingCache.get(Object)
which will
"load" the value if it's not present, thus populating the key.k
- K
if cached or loaded.
See LoadingCache.get(Object)
for possible exceptions.public void invalidate()
invalidate
in interface AuthCacheMBean
public void invalidate(K k)
k
- key to invalidatepublic void setValidity(int validityPeriod)
setValidity
in interface AuthCacheMBean
validityPeriod
- in millisecondspublic int getValidity()
getValidity
in interface AuthCacheMBean
public void setUpdateInterval(int updateInterval)
setUpdateInterval
in interface AuthCacheMBean
updateInterval
- in millisecondspublic int getUpdateInterval()
getUpdateInterval
in interface AuthCacheMBean
public void setMaxEntries(int maxEntries)
setMaxEntries
in interface AuthCacheMBean
maxEntries
- public int getMaxEntries()
getMaxEntries
in interface AuthCacheMBean
public boolean getActiveUpdate()
getActiveUpdate
in interface AuthCacheMBean
public void setActiveUpdate(boolean update)
setActiveUpdate
in interface AuthCacheMBean
public long getEstimatedSize()
getEstimatedSize
in interface AuthCacheMBean
protected com.github.benmanes.caffeine.cache.LoadingCache<K,V> initCache(com.github.benmanes.caffeine.cache.LoadingCache<K,V> existing)
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.existing
- If not null will only update cache update validity, max entries, and update interval.LoadingCache
if existing was null, otherwise the existing cache
public boolean isTerminated()
isTerminated
in interface Shutdownable
public void shutdown()
Shutdownable
shutdown
in interface Shutdownable
public java.lang.Object shutdownNow()
Shutdownable
shutdownNow
in interface Shutdownable
public boolean awaitTermination(long timeout, java.util.concurrent.TimeUnit units) throws java.lang.InterruptedException
Shutdownable
awaitTermination
in interface Shutdownable
java.lang.InterruptedException
public void warm()
Copyright © 2009- The Apache Software Foundation