Class CacheProxy<K,V>
java.lang.Object
com.github.benmanes.caffeine.jcache.CacheProxy<K,V>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<javax.cache.Cache.Entry<K,
,V>> javax.cache.Cache<K,
V>
- Direct Known Subclasses:
LoadingCacheProxy
An implementation of JSR-107
Cache
backed by a Caffeine cache.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final class
An iterator to safely expose the cache entries.(package private) static enum
Nested classes/interfaces inherited from interface javax.cache.Cache
javax.cache.Cache.Entry<K,
V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final javax.cache.CacheManager
(package private) final JCacheMXBean
(package private) boolean
(package private) final CaffeineConfiguration
<K, V> (package private) final Copier
protected final EventDispatcher
<K, V> protected final Executor
protected final javax.cache.expiry.ExpiryPolicy
protected final Set
<CompletableFuture<?>> private static final Logger
(package private) final String
protected final JCacheStatisticsMXBean
protected final Ticker
-
Constructor Summary
ConstructorsConstructorDescriptionCacheProxy
(String name, Executor executor, javax.cache.CacheManager cacheManager, CaffeineConfiguration<K, V> configuration, Cache<K, Expirable<V>> cache, EventDispatcher<K, V> dispatcher, Optional<javax.cache.integration.CacheLoader<K, V>> cacheLoader, javax.cache.expiry.ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics) -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
close()
boolean
containsKey
(K key) Returns a deep copy of the map if value-based caching is enabled.protected final <T> @NonNull T
copyOf
(@Nullable T object) Returns a copy of the value if value-based caching is enabled.protected final @Nullable V
Returns a copy of the value if value-based caching is enabled.protected final long
private @Nullable javax.cache.integration.CacheWriterException
deleteAllToCacheWriter
(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
getAndFilterExpiredEntries
(Set<? extends K> keys, boolean updateAccessTime) Returns all of the mappings present, expiring as required, and optionally updates their access expiry time.@Nullable V
getAndRemove
(K key) getAndReplace
(K key, V value) javax.cache.CacheManager
getConfiguration
(Class<C> clazz) getName()
protected final long
getWriteExpireTimeMS
(boolean created) Returns the time when the entry will expire.<T> @Nullable T
invokeAll
(Set<? extends K> keys, javax.cache.processor.EntryProcessor<K, V, T> entryProcessor, Object... arguments) boolean
isClosed()
iterator()
void
loadAll
(Set<? extends K> keys, boolean replaceExistingValues, javax.cache.integration.CompletionListener completionListener) private void
loadAllAndKeepExisting
(Set<? extends K> keys) Performs the bulk load where the existing entries are retained.private void
loadAllAndReplaceExisting
(Set<? extends K> keys) Performs the bulk load where the existing entries are replace.protected static long
nanosToMillis
(long nanos) 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
(Consumer<T> action, Supplier<T> data) Performs the action with the cache writer if write-through is enabled.void
void
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
boolean
void
void
private V
removeNoCopyOrAwait
(K key) Removes the mapping from the cache without store-by-value copying nor waiting for synchronous listeners to complete.boolean
boolean
private V
replaceNoCopyOrAwait
(K key, V value) Replaces the entry for the specified key only if it is currently mapped to some value.protected final void
Checks that the cache is not closed.protected final void
setAccessExpirationTime
(K key, Expirable<?> expirable, long currentTimeMS) Sets the access expiration time.private @Nullable Throwable
private static @Nullable Throwable
Attempts to close the resource.<T> T
private @Nullable javax.cache.integration.CacheWriterException
writeAllToCacheWriter
(Map<? extends K, ? extends V> map) Writes all of the entries to the cache writer if write-through is enabled.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
logger
-
cache
-
configuration
-
cacheManager
final javax.cache.CacheManager cacheManager -
writer
-
cacheMXBean
-
copier
-
name
-
cacheLoader
-
inFlight
-
statistics
-
dispatcher
-
expiry
protected final javax.cache.expiry.ExpiryPolicy expiry -
executor
-
ticker
-
closed
volatile boolean closed
-
-
Constructor Details
-
CacheProxy
public CacheProxy(String name, Executor executor, javax.cache.CacheManager cacheManager, CaffeineConfiguration<K, V> configuration, Cache<K, Expirable<V>> cache, EventDispatcher<K, V> dispatcher, Optional<javax.cache.integration.CacheLoader<K, V>> cacheLoader, javax.cache.expiry.ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics)
-
-
Method Details
-
containsKey
-
get
-
getAll
-
getAndFilterExpiredEntries
protected Map<K,Expirable<V>> getAndFilterExpiredEntries(Set<? extends K> keys, boolean updateAccessTime) Returns all of the mappings present, expiring as required, and optionally updates their access expiry time. -
loadAll
-
loadAllAndReplaceExisting
Performs the bulk load where the existing entries are replace. -
loadAllAndKeepExisting
Performs the bulk load where the existing entries are retained. -
put
-
getAndPut
-
putNoCopyOrAwait
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
-
putAll
-
putIfAbsent
-
putIfAbsentNoAwait
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
-
remove
-
removeNoCopyOrAwait
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
-
remove
-
getAndRemove
-
replace
-
replace
-
getAndReplace
-
replaceNoCopyOrAwait
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
-
removeAll
public void removeAll() -
clear
public void clear() -
getConfiguration
-
getConfiguration
- Returns:
- the cache's configuration
-
invoke
-
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
-
getName
-
getCacheManager
public javax.cache.CacheManager getCacheManager() -
isClosed
public boolean isClosed() -
close
public void close() -
shutdownExecutor
-
tryClose
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
-
registerCacheEntryListener
-
deregisterCacheEntryListener
-
iterator
-
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
Performs the action with the cache writer if write-through is enabled. -
writeAllToCacheWriter
private @Nullable javax.cache.integration.CacheWriterException writeAllToCacheWriter(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(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
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
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
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
-