Class CacheProxy<K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.jcache.CacheProxy<K,V>
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Iterable<javax.cache.Cache.Entry<K,V>>
,javax.cache.Cache<K,V>
- Direct Known Subclasses:
LoadingCacheProxy
public class CacheProxy<K,V> extends java.lang.Object implements javax.cache.Cache<K,V>
An implementation of JSR-107Cache
backed by a Caffeine cache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
CacheProxy.EntryIterator
An iterator to safely expose the cache entries.(package private) static class
CacheProxy.NullCompletionListener
-
Field Summary
Fields Modifier and Type Field Description (package private) Cache<K,Expirable<V>>
cache
protected java.util.Optional<javax.cache.integration.CacheLoader<K,V>>
cacheLoader
(package private) javax.cache.CacheManager
cacheManager
(package private) JCacheMXBean
cacheMXBean
(package private) boolean
closed
(package private) CaffeineConfiguration<K,V>
configuration
(package private) Copier
copier
protected EventDispatcher<K,V>
dispatcher
protected java.util.concurrent.Executor
executor
protected javax.cache.expiry.ExpiryPolicy
expiry
protected java.util.Set<java.util.concurrent.CompletableFuture<?>>
inFlight
private static java.util.logging.Logger
logger
(package private) java.lang.String
name
protected JCacheStatisticsMXBean
statistics
protected Ticker
ticker
(package private) javax.cache.integration.CacheWriter<K,V>
writer
-
Constructor Summary
Constructors Constructor Description CacheProxy(java.lang.String name, java.util.concurrent.Executor executor, javax.cache.CacheManager cacheManager, CaffeineConfiguration<K,V> configuration, Cache<K,Expirable<V>> cache, EventDispatcher<K,V> dispatcher, java.util.Optional<javax.cache.integration.CacheLoader<K,V>> cacheLoader, javax.cache.expiry.ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
void
close()
boolean
containsKey(K key)
protected java.util.Map<K,V>
copyMap(java.util.Map<K,Expirable<V>> map)
Returns a deep copy of the map if value-based caching is enabled.protected <T> @NonNull T
copyOf(@Nullable T object)
Returns a copy of the value if value-based caching is enabled.protected @Nullable V
copyValue(@Nullable Expirable<V> expirable)
Returns a copy of the value if value-based caching is enabled.protected long
currentTimeMillis()
private @Nullable javax.cache.integration.CacheWriterException
deleteAllToCacheWriter(java.util.Set<? extends K> keys)
Deletes all of the entries using the cache writer, retaining only the keys that succeeded.void
deregisterCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
(package private) void
enableManagement(boolean enabled)
Enables or disables the configuration management JMX bean.(package private) void
enableStatistics(boolean enabled)
Enables or disables the statistics JMX bean.@Nullable V
get(K key)
java.util.Map<K,V>
getAll(java.util.Set<? extends K> keys)
protected java.util.Map<K,Expirable<V>>
getAndFilterExpiredEntries(java.util.Set<? extends K> keys, boolean updateAccessTime)
Returns all of the mappings present, expiring as required, and optionally updates their access expiry time.@Nullable V
getAndPut(K key, V value)
V
getAndRemove(K key)
V
getAndReplace(K key, V value)
javax.cache.CacheManager
getCacheManager()
CaffeineConfiguration<K,V>
getConfiguration()
<C extends javax.cache.configuration.Configuration<K,V>>
CgetConfiguration(java.lang.Class<C> clazz)
java.lang.String
getName()
protected long
getWriteExpireTimeMS(boolean created)
Returns the time when the entry will expire.<T> @Nullable T
invoke(K key, javax.cache.processor.EntryProcessor<K,V,T> entryProcessor, java.lang.Object... arguments)
<T> java.util.Map<K,javax.cache.processor.EntryProcessorResult<T>>
invokeAll(java.util.Set<? extends K> keys, javax.cache.processor.EntryProcessor<K,V,T> entryProcessor, java.lang.Object... arguments)
boolean
isClosed()
java.util.Iterator<javax.cache.Cache.Entry<K,V>>
iterator()
void
loadAll(java.util.Set<? extends K> keys, boolean replaceExistingValues, javax.cache.integration.CompletionListener completionListener)
private void
loadAllAndKeepExisting(java.util.Set<? extends K> keys)
Performs the bulk load where the existing entries are retained.private void
loadAllAndReplaceExisting(java.util.Set<? extends K> keys)
Performs the bulk load where the existing entries are replace.protected static long
nanosToMillis(long nanos)
private @Nullable Expirable<V>
postProcess(Expirable<V> expirable, EntryProcessorEntry<K,V> entry, long currentTimeMS)
Returns the updated expirable value after performing the post processing actions.private <T> void
publishToCacheWriter(java.util.function.Consumer<T> action, java.util.function.Supplier<T> data)
Performs the action with the cache writer if write-through is enabled.void
put(K key, V value)
void
putAll(java.util.Map<? extends K,? extends V> map)
boolean
putIfAbsent(K key, V value)
private boolean
putIfAbsentNoAwait(K key, V value, boolean publishToWriter)
Associates the specified value with the specified key in the cache if there is no existing mapping.protected V
putNoCopyOrAwait(K key, V value, boolean publishToWriter, int[] puts)
Associates the specified value with the specified key in the cache.void
registerCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
boolean
remove(K key)
boolean
remove(K key, V oldValue)
void
removeAll()
void
removeAll(java.util.Set<? extends K> keys)
private V
removeNoCopyOrAwait(K key)
Removes the mapping from the cache without store-by-value copying nor waiting for synchronous listeners to complete.boolean
replace(K key, V value)
boolean
replace(K key, V oldValue, V newValue)
private V
replaceNoCopyOrAwait(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.protected void
requireNotClosed()
Checks that the cache is not closed.protected void
setAccessExpirationTime(K key, Expirable<?> expirable, long currentTimeMS)
Sets the access expiration time.private @Nullable java.lang.Throwable
shutdownExecutor()
private static @Nullable java.lang.Throwable
tryClose(@Nullable java.lang.Object o, @Nullable java.lang.Throwable outer)
Attempts to close the resource.<T> T
unwrap(java.lang.Class<T> clazz)
private @Nullable javax.cache.integration.CacheWriterException
writeAllToCacheWriter(java.util.Map<? extends K,? extends V> map)
Writes all of the entries to the cache writer if write-through is enabled.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
configuration
final CaffeineConfiguration<K,V> configuration
-
cacheManager
final javax.cache.CacheManager cacheManager
-
cacheMXBean
final JCacheMXBean cacheMXBean
-
copier
final Copier copier
-
name
final java.lang.String name
-
cacheLoader
protected final java.util.Optional<javax.cache.integration.CacheLoader<K,V>> cacheLoader
-
inFlight
protected final java.util.Set<java.util.concurrent.CompletableFuture<?>> inFlight
-
statistics
protected final JCacheStatisticsMXBean statistics
-
dispatcher
protected final EventDispatcher<K,V> dispatcher
-
expiry
protected final javax.cache.expiry.ExpiryPolicy expiry
-
executor
protected final java.util.concurrent.Executor executor
-
ticker
protected final Ticker ticker
-
closed
volatile boolean closed
-
-
Constructor Detail
-
CacheProxy
public CacheProxy(java.lang.String name, java.util.concurrent.Executor executor, javax.cache.CacheManager cacheManager, CaffeineConfiguration<K,V> configuration, Cache<K,Expirable<V>> cache, EventDispatcher<K,V> dispatcher, java.util.Optional<javax.cache.integration.CacheLoader<K,V>> cacheLoader, javax.cache.expiry.ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics)
-
-
Method Detail
-
containsKey
public boolean containsKey(K key)
-
getAndFilterExpiredEntries
protected java.util.Map<K,Expirable<V>> getAndFilterExpiredEntries(java.util.Set<? extends K> keys, boolean updateAccessTime)
Returns all of the mappings present, expiring as required, and optionally updates their access expiry time.
-
loadAll
public void loadAll(java.util.Set<? extends K> keys, boolean replaceExistingValues, javax.cache.integration.CompletionListener completionListener)
-
loadAllAndReplaceExisting
private void loadAllAndReplaceExisting(java.util.Set<? extends K> keys)
Performs the bulk load where the existing entries are replace.
-
loadAllAndKeepExisting
private void loadAllAndKeepExisting(java.util.Set<? extends K> keys)
Performs the bulk load where the existing entries are retained.
-
putNoCopyOrAwait
protected V putNoCopyOrAwait(K key, V value, boolean publishToWriter, int[] puts)
Associates the specified value with the specified key in the cache.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keypublishToWriter
- if the writer should be notifiedputs
- the accumulator for additions and updates- Returns:
- the old value
-
putIfAbsentNoAwait
private boolean putIfAbsentNoAwait(K key, V value, boolean publishToWriter)
Associates the specified value with the specified key in the cache if there is no existing mapping.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keypublishToWriter
- if the writer should be notified- Returns:
- if the mapping was successful
-
removeNoCopyOrAwait
private V removeNoCopyOrAwait(K key)
Removes the mapping from the cache without store-by-value copying nor waiting for synchronous listeners to complete.- Parameters:
key
- key whose mapping is to be removed from the cache- Returns:
- the old value
-
replaceNoCopyOrAwait
private V replaceNoCopyOrAwait(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value. The entry is not store-by-value copied nor does the method wait for synchronous listeners to complete.- Parameters:
key
- key with which the specified value is associatedvalue
- value to be associated with the specified key- Returns:
- the old value
-
removeAll
public void removeAll(java.util.Set<? extends K> keys)
-
getConfiguration
public <C extends javax.cache.configuration.Configuration<K,V>> C getConfiguration(java.lang.Class<C> clazz)
-
getConfiguration
public CaffeineConfiguration<K,V> getConfiguration()
- Returns:
- the cache's configuration
-
invoke
public <T> @Nullable T invoke(K key, javax.cache.processor.EntryProcessor<K,V,T> entryProcessor, java.lang.Object... arguments)
-
postProcess
private @Nullable Expirable<V> postProcess(Expirable<V> expirable, EntryProcessorEntry<K,V> entry, long currentTimeMS)
Returns the updated expirable value after performing the post processing actions.
-
invokeAll
public <T> java.util.Map<K,javax.cache.processor.EntryProcessorResult<T>> invokeAll(java.util.Set<? extends K> keys, javax.cache.processor.EntryProcessor<K,V,T> entryProcessor, java.lang.Object... arguments)
-
getName
public java.lang.String getName()
-
getCacheManager
public javax.cache.CacheManager getCacheManager()
-
close
public void close()
-
shutdownExecutor
private @Nullable java.lang.Throwable shutdownExecutor()
-
tryClose
private static @Nullable java.lang.Throwable tryClose(@Nullable java.lang.Object o, @Nullable java.lang.Throwable outer)
Attempts to close the resource. If an error occurs and an outermost exception is set, then adds the error to the suppression list.- Parameters:
o
- the resource to close if Closeableouter
- the outermost error, or null if unset- Returns:
- the outermost error, or null if unset and successful
-
unwrap
public <T> T unwrap(java.lang.Class<T> clazz)
-
registerCacheEntryListener
public void registerCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
-
deregisterCacheEntryListener
public void deregisterCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
-
enableManagement
void enableManagement(boolean enabled)
Enables or disables the configuration management JMX bean.
-
enableStatistics
void enableStatistics(boolean enabled)
Enables or disables the statistics JMX bean.
-
publishToCacheWriter
private <T> void publishToCacheWriter(java.util.function.Consumer<T> action, java.util.function.Supplier<T> data)
Performs the action with the cache writer if write-through is enabled.
-
writeAllToCacheWriter
private @Nullable javax.cache.integration.CacheWriterException writeAllToCacheWriter(java.util.Map<? extends K,? extends V> map)
Writes all of the entries to the cache writer if write-through is enabled.
-
deleteAllToCacheWriter
private @Nullable javax.cache.integration.CacheWriterException deleteAllToCacheWriter(java.util.Set<? extends K> keys)
Deletes all of the entries using the cache writer, retaining only the keys that succeeded.
-
requireNotClosed
protected final void requireNotClosed()
Checks that the cache is not closed.
-
copyOf
protected final <T> @NonNull T copyOf(@Nullable T object)
Returns a copy of the value if value-based caching is enabled.- Type Parameters:
T
- the type of object being copied- Parameters:
object
- the object to be copied- Returns:
- a copy of the object if storing by value or the same instance if by reference
-
copyValue
protected final @Nullable V copyValue(@Nullable Expirable<V> expirable)
Returns a copy of the value if value-based caching is enabled.- Parameters:
expirable
- the expirable value to be copied- Returns:
- a copy of the value if storing by value or the same instance if by reference
-
copyMap
protected final java.util.Map<K,V> copyMap(java.util.Map<K,Expirable<V>> map)
Returns a deep copy of the map if value-based caching is enabled.- Parameters:
map
- the mapping of keys to expirable values- Returns:
- a deep or shallow copy of the mappings depending on the store by value setting
-
currentTimeMillis
protected final long currentTimeMillis()
- Returns:
- the current time in milliseconds
-
nanosToMillis
protected static long nanosToMillis(long nanos)
- Returns:
- the nanosecond time in milliseconds
-
setAccessExpirationTime
protected final void setAccessExpirationTime(K key, Expirable<?> expirable, long currentTimeMS)
Sets the access expiration time.- Parameters:
key
- the entry's keyexpirable
- the entry that was operated oncurrentTimeMS
- the current time, or 0 if not read yet
-
getWriteExpireTimeMS
protected final long getWriteExpireTimeMS(boolean created)
Returns the time when the entry will expire.- Parameters:
created
- if the write is an insert or update- Returns:
- the time when the entry will expire, zero if it should expire immediately, Long.MIN_VALUE if it should not be changed, or Long.MAX_VALUE if eternal
-
-