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-107Cachebacked by a Caffeine cache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classCacheProxy.NullCompletionListener
-
Field Summary
Fields Modifier and Type Field Description protected Cache<K,Expirable<V>>cacheprotected Optional<CacheLoader<K,V>>cacheLoaderprotected EventDispatcher<K,V>dispatcherprotected Executorexecutorprotected Set<CompletableFuture<?>>inFlightprotected JCacheStatisticsMXBeanstatisticsprotected Tickerticker
-
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 voidclear()voidclose()booleancontainsKey(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 TcopyOf(@PolyNull @Nullable T object)Returns a copy of the value if value-based caching is enabled.protected @PolyNull VcopyValue(@PolyNull Expirable<V> expirable)Returns a copy of the value if value-based caching is enabled.protected longcurrentTimeMillis()Returns the current time in milliseconds.voidderegisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)@Nullable Vget(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 VgetAndPut(K key, V value)VgetAndRemove(K key)VgetAndReplace(K key, V value)CacheManagergetCacheManager()<C extends Configuration<K,V>>
CgetConfiguration(Class<C> clazz)StringgetName()protected longgetWriteExpireTimeMS(boolean created)Returns the time when the entry will expire.<T> @Nullable Tinvoke(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)booleanisClosed()Iterator<Cache.Entry<K,V>>iterator()voidloadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener)protected static longnanosToMillis(long nanos)Returns the nanosecond time in milliseconds.voidput(K key, V value)voidputAll(Map<? extends K,? extends V> map)booleanputIfAbsent(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.voidregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)booleanremove(K key)booleanremove(K key, V oldValue)voidremoveAll()voidremoveAll(Set<? extends K> keys)booleanreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)protected voidrequireNotClosed()Checks that the cache is not closed.protected voidsetAccessExpireTime(K key, Expirable<?> expirable, long currentTimeMS)Sets the access expiration time.<T> Tunwrap(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:
containsKeyin 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:
putIfAbsentin 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:
getAndReplacein interfaceCache<K,V>
-
getConfiguration
public <C extends Configuration<K,V>> C getConfiguration(Class<C> clazz)
- Specified by:
getConfigurationin 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:
getCacheManagerin interfaceCache<K,V>
-
close
public void close()
-
registerCacheEntryListener
public void registerCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
- Specified by:
registerCacheEntryListenerin interfaceCache<K,V>
-
deregisterCacheEntryListener
public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
- Specified by:
deregisterCacheEntryListenerin 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
-
-