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

public class CacheProxy<K,V> extends Object implements javax.cache.Cache<K,V>
An implementation of JSR-107 Cache backed by a Caffeine cache.
  • Field Details

    • logger

      private static final Logger logger
    • cache

      final Cache<K,Expirable<V>> cache
    • configuration

      final CaffeineConfiguration<K,V> configuration
    • cacheManager

      final javax.cache.CacheManager cacheManager
    • writer

      final javax.cache.integration.CacheWriter<K,V> writer
    • cacheMXBean

      final JCacheMXBean cacheMXBean
    • copier

      final Copier copier
    • name

      final String name
    • cacheLoader

      protected final Optional<javax.cache.integration.CacheLoader<K,V>> cacheLoader
    • inFlight

      protected final Set<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 Executor executor
    • ticker

      protected final Ticker ticker
    • closed

      volatile boolean closed
  • Constructor Details

  • Method Details

    • containsKey

      public boolean containsKey(K key)
      Specified by:
      containsKey in interface javax.cache.Cache<K,V>
    • get

      public @Nullable V get(K key)
      Specified by:
      get in interface javax.cache.Cache<K,V>
    • getAll

      public Map<K,V> getAll(Set<? extends K> keys)
      Specified by:
      getAll in interface javax.cache.Cache<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, javax.cache.integration.CompletionListener completionListener)
      Specified by:
      loadAll in interface javax.cache.Cache<K,V>
    • loadAllAndReplaceExisting

      private void loadAllAndReplaceExisting(Set<? extends K> keys)
      Performs the bulk load where the existing entries are replace.
    • loadAllAndKeepExisting

      private void loadAllAndKeepExisting(Set<? extends K> keys)
      Performs the bulk load where the existing entries are retained.
    • put

      public void put(K key, V value)
      Specified by:
      put in interface javax.cache.Cache<K,V>
    • getAndPut

      public @Nullable V getAndPut(K key, V value)
      Specified by:
      getAndPut in interface javax.cache.Cache<K,V>
    • 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 associated
      value - value to be associated with the specified key
      publishToWriter - if the writer should be notified
      puts - the accumulator for additions and updates
      Returns:
      the old value
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      Specified by:
      putAll in interface javax.cache.Cache<K,V>
    • putIfAbsent

      public boolean putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface javax.cache.Cache<K,V>
    • 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 associated
      value - value to be associated with the specified key
      publishToWriter - if the writer should be notified
      Returns:
      if the mapping was successful
    • remove

      public boolean remove(K key)
      Specified by:
      remove in interface javax.cache.Cache<K,V>
    • 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
    • remove

      public boolean remove(K key, V oldValue)
      Specified by:
      remove in interface javax.cache.Cache<K,V>
    • getAndRemove

      public V getAndRemove(K key)
      Specified by:
      getAndRemove in interface javax.cache.Cache<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface javax.cache.Cache<K,V>
    • replace

      public boolean replace(K key, V value)
      Specified by:
      replace in interface javax.cache.Cache<K,V>
    • getAndReplace

      public V getAndReplace(K key, V value)
      Specified by:
      getAndReplace in interface javax.cache.Cache<K,V>
    • 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 associated
      value - value to be associated with the specified key
      Returns:
      the old value
    • removeAll

      public void removeAll(Set<? extends K> keys)
      Specified by:
      removeAll in interface javax.cache.Cache<K,V>
    • removeAll

      public void removeAll()
      Specified by:
      removeAll in interface javax.cache.Cache<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface javax.cache.Cache<K,V>
    • getConfiguration

      public <C extends javax.cache.configuration.Configuration<K, V>> C getConfiguration(Class<C> clazz)
      Specified by:
      getConfiguration in interface javax.cache.Cache<K,V>
    • 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, Object... arguments)
      Specified by:
      invoke in interface javax.cache.Cache<K,V>
    • 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> Map<K,javax.cache.processor.EntryProcessorResult<T>> invokeAll(Set<? extends K> keys, javax.cache.processor.EntryProcessor<K,V,T> entryProcessor, Object... arguments)
      Specified by:
      invokeAll in interface javax.cache.Cache<K,V>
    • getName

      public String getName()
      Specified by:
      getName in interface javax.cache.Cache<K,V>
    • getCacheManager

      public javax.cache.CacheManager getCacheManager()
      Specified by:
      getCacheManager in interface javax.cache.Cache<K,V>
    • isClosed

      public boolean isClosed()
      Specified by:
      isClosed in interface javax.cache.Cache<K,V>
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface javax.cache.Cache<K,V>
      Specified by:
      close in interface Closeable
    • shutdownExecutor

      private @Nullable Throwable shutdownExecutor()
    • tryClose

      private static @Nullable Throwable tryClose(@Nullable Object o, @Nullable 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 Closeable
      outer - the outermost error, or null if unset
      Returns:
      the outermost error, or null if unset and successful
    • unwrap

      public <T> T unwrap(Class<T> clazz)
      Specified by:
      unwrap in interface javax.cache.Cache<K,V>
    • registerCacheEntryListener

      public void registerCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
      Specified by:
      registerCacheEntryListener in interface javax.cache.Cache<K,V>
    • deregisterCacheEntryListener

      public void deregisterCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration)
      Specified by:
      deregisterCacheEntryListener in interface javax.cache.Cache<K,V>
    • iterator

      public Iterator<javax.cache.Cache.Entry<K,V>> iterator()
      Specified by:
      iterator in interface javax.cache.Cache<K,V>
      Specified by:
      iterator in interface Iterable<K>
    • 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(Consumer<T> action, Supplier<T> data)
      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

      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 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
    • setAccessExpirationTime

      protected final void setAccessExpirationTime(K key, Expirable<?> expirable, long currentTimeMS)
      Sets the access expiration time.
      Parameters:
      key - the entry's key
      expirable - the entry that was operated on
      currentTimeMS - 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