Class CacheProxy<K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.jcache.CacheProxy<K,V>
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<Cache.Entry<K,V>>
,Cache<K,V>
- Direct Known Subclasses:
LoadingCacheProxy
public class CacheProxy<K,V> extends Object implements Cache<K,V>
An implementation of JSR-107Cache
backed by a Caffeine cache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
CacheProxy.NullCompletionListener
-
Field Summary
Fields Modifier and Type Field Description protected Cache<K,Expirable<V>>
cache
protected Optional<CacheLoader<K,V>>
cacheLoader
protected EventDispatcher<K,V>
dispatcher
protected Executor
executor
protected Set<CompletableFuture<?>>
inFlight
protected JCacheStatisticsMXBean
statistics
protected Ticker
ticker
-
Constructor Summary
Constructors Constructor Description CacheProxy(String name, Executor executor, CacheManager cacheManager, CaffeineConfiguration<K,V> configuration, Cache<K,Expirable<V>> cache, EventDispatcher<K,V> dispatcher, Optional<CacheLoader<K,V>> cacheLoader, 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 Map<K,V>
copyMap(Map<K,Expirable<V>> map)
Returns a deep copy of the map if value-based caching is enabled.protected <T> @PolyNull T
copyOf(@PolyNull @Nullable T object)
Returns a copy of the value if value-based caching is enabled.protected @PolyNull V
copyValue(@PolyNull Expirable<V> expirable)
Returns a copy of the value if value-based caching is enabled.protected long
currentTimeMillis()
Returns the current time in milliseconds.void
deregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
@Nullable V
get(K key)
Map<K,V>
getAll(Set<? extends K> keys)
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.@Nullable V
getAndPut(K key, V value)
V
getAndRemove(K key)
V
getAndReplace(K key, V value)
CacheManager
getCacheManager()
<C extends Configuration<K,V>>
CgetConfiguration(Class<C> clazz)
String
getName()
protected long
getWriteExpireTimeMS(boolean created)
Returns the time when the entry will expire.<T> @Nullable T
invoke(K key, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
<T> Map<K,EntryProcessorResult<T>>
invokeAll(Set<? extends K> keys, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
boolean
isClosed()
Iterator<Cache.Entry<K,V>>
iterator()
void
loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener)
protected static long
nanosToMillis(long nanos)
Returns the nanosecond time in milliseconds.void
put(K key, V value)
void
putAll(Map<? extends K,? extends V> map)
boolean
putIfAbsent(K key, V value)
protected com.github.benmanes.caffeine.jcache.CacheProxy.PutResult<V>
putNoCopyOrAwait(K key, V value, boolean publishToWriter)
Associates the specified value with the specified key in the cache.void
registerCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
boolean
remove(K key)
boolean
remove(K key, V oldValue)
void
removeAll()
void
removeAll(Set<? extends K> keys)
boolean
replace(K key, V value)
boolean
replace(K key, V oldValue, V newValue)
protected void
requireNotClosed()
Checks that the cache is not closed.protected void
setAccessExpireTime(K key, Expirable<?> expirable, long currentTimeMS)
Sets the access expiration time.<T> T
unwrap(Class<T> clazz)
-
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 Detail
-
cacheLoader
protected final Optional<CacheLoader<K,V>> cacheLoader
-
inFlight
protected final Set<CompletableFuture<?>> inFlight
-
statistics
protected final JCacheStatisticsMXBean statistics
-
dispatcher
protected final EventDispatcher<K,V> dispatcher
-
executor
protected final Executor executor
-
ticker
protected final Ticker ticker
-
-
Constructor Detail
-
CacheProxy
public CacheProxy(String name, Executor executor, CacheManager cacheManager, CaffeineConfiguration<K,V> configuration, Cache<K,Expirable<V>> cache, EventDispatcher<K,V> dispatcher, Optional<CacheLoader<K,V>> cacheLoader, ExpiryPolicy expiry, Ticker ticker, JCacheStatisticsMXBean statistics)
-
-
Method Detail
-
containsKey
public boolean containsKey(K key)
- Specified by:
containsKey
in interfaceCache<K,V>
-
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
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener)
-
putNoCopyOrAwait
@CanIgnoreReturnValue protected com.github.benmanes.caffeine.jcache.CacheProxy.PutResult<V> putNoCopyOrAwait(K key, V value, boolean publishToWriter)
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 notified- Returns:
- the old value
-
putIfAbsent
@CanIgnoreReturnValue public boolean putIfAbsent(K key, V value)
- Specified by:
putIfAbsent
in interfaceCache<K,V>
-
remove
@CanIgnoreReturnValue public boolean remove(K key)
-
remove
@CanIgnoreReturnValue public boolean remove(K key, V oldValue)
-
replace
@CanIgnoreReturnValue public boolean replace(K key, V oldValue, V newValue)
-
replace
@CanIgnoreReturnValue public boolean replace(K key, V value)
-
getAndReplace
public V getAndReplace(K key, V value)
- Specified by:
getAndReplace
in interfaceCache<K,V>
-
getConfiguration
public <C extends Configuration<K,V>> C getConfiguration(Class<C> clazz)
- Specified by:
getConfiguration
in interfaceCache<K,V>
-
invoke
public <T> @Nullable T invoke(K key, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
-
invokeAll
public <T> Map<K,EntryProcessorResult<T>> invokeAll(Set<? extends K> keys, EntryProcessor<K,V,T> entryProcessor, Object... arguments)
-
getCacheManager
public CacheManager getCacheManager()
- Specified by:
getCacheManager
in interfaceCache<K,V>
-
close
public void close()
-
registerCacheEntryListener
public void registerCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
- Specified by:
registerCacheEntryListener
in interfaceCache<K,V>
-
deregisterCacheEntryListener
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
- Specified by:
deregisterCacheEntryListener
in interfaceCache<K,V>
-
iterator
public Iterator<Cache.Entry<K,V>> iterator()
-
requireNotClosed
protected final void requireNotClosed()
Checks that the cache is not closed.
-
copyOf
protected final <T> @PolyNull T copyOf(@PolyNull @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 @PolyNull V copyValue(@PolyNull 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 Map<K,V> copyMap(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.
-
setAccessExpireTime
protected final void setAccessExpireTime(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 operation is an insert or an 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
-
-