Class BufferingDelegatingCache<K,V>

java.lang.Object
net.solarnetwork.central.support.BufferingDelegatingCache<K,V>
All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<javax.cache.Cache.Entry<K,V>>, javax.cache.Cache<K,V>

public class BufferingDelegatingCache<K,V> extends Object implements javax.cache.Cache<K,V>
Cache implementation that uses an in-memory only store for up to a maximum number of keys, then delegates operations to another Cache for overflow.

This cache has been designed to help minimize the cost required to serialize/deserialize objects as they are placed into an off-heap cache implementation. Keys and values are not serialized while maintained internally to this cache. It can be useful if a cache is mostly being used for temporary persistence where keys are added and removed regularly, and the expected size of elements mostly stays below a certain threshold but sometimes pushes beyond and the cache must accommodate. In that situation, this cache would be configured with an internalCapacity of the expected threshold, so that most of the time cached elements are managed internally and occasionally excess elements would be pushed into the delegate cache instance.

Note that not null values are not supported. Not all Cache methods are supported, and will throw an UnsupportedOperationException if invoked. Additionally only one CacheEntryListener for each of created, updated, and removed events is supported, when calling registerCacheEntryListener(CacheEntryListenerConfiguration).

Since:
2.2
  • Constructor Details

    • BufferingDelegatingCache

      public BufferingDelegatingCache(javax.cache.Cache<K,V> delegate, int internalCapacity)
      Constructor.

      A ConcurrentHashMap will be created for the internal store.

      Parameters:
      delegate - the cache to delegate elements to once more than internalCapacity elemements are managed by the cache
      internalCapacity - the maximum number of elements to keep in the internal store
      See Also:
    • BufferingDelegatingCache

      public BufferingDelegatingCache(javax.cache.Cache<K,V> delegate, int internalCapacity, ConcurrentMap<K,V> internalStore)
      Constructor.
      Parameters:
      delegate - the cache to delegate elements to once more than internalCapacity elements are managed by the cache
      internalCapacity - the maximum number of elements to keep in the internal store
      internalStore - the internal store to use
  • Method Details

    • getInternalCapacity

      public int getInternalCapacity()
      Get the configured internal capacity.
      Returns:
      the number of elements that can be stored internally, before passing them to the delegate Cache
    • getInternalSize

      public int getInternalSize()
      Get the number of elements currently stored internally.
      Returns:
      the internal element count
    • getInternalSizeWatermark

      public int getInternalSizeWatermark()
      Get the highest number of elements stored internally, since the last reset via clear().
      Returns:
      the highest number of elements stored internally
      Since:
      1.1
    • clear

      public void clear()
      Specified by:
      clear 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
    • containsKey

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

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

      public 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>
    • getAndPut

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

      public V getAndRemove(K key)
      Specified by:
      getAndRemove 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>
    • getCacheManager

      public javax.cache.CacheManager getCacheManager()
      Specified by:
      getCacheManager 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>
    • getName

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

      public <T> T invoke(K key, javax.cache.processor.EntryProcessor<K,V,T> processor, Object... arg2) throws javax.cache.processor.EntryProcessorException
      Specified by:
      invoke in interface javax.cache.Cache<K,V>
      Throws:
      javax.cache.processor.EntryProcessorException
    • invokeAll

      public <T> Map<K,javax.cache.processor.EntryProcessorResult<T>> invokeAll(Set<? extends K> arg0, javax.cache.processor.EntryProcessor<K,V,T> arg1, Object... arg2)
      Specified by:
      invokeAll in interface javax.cache.Cache<K,V>
    • isClosed

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

      public Iterator<javax.cache.Cache.Entry<K,V>> iterator()
      Get an iterator over all cache elements.

      The iterator returned by this method will return the cache elements stored internally first, followed by any elements stored in the delegate Cache.

      Specified by:
      iterator in interface javax.cache.Cache<K,V>
      Specified by:
      iterator in interface Iterable<K>
    • loadAll

      public void loadAll(Set<? extends K> keys, boolean arg1, javax.cache.integration.CompletionListener arg2)
      Specified by:
      loadAll in interface javax.cache.Cache<K,V>
    • put

      public void put(K key, V value)
      Add an element to the cache.

      If less than getInternalCapacity() elements are in the cache when this is invoked, the element will be added to the internal store. Otherwise it will be put into the delegate Cache.

      Specified by:
      put in interface javax.cache.Cache<K,V>
    • 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)
      Add an element to the cache only if its key does not already exist in the cache.
      Specified by:
      putIfAbsent in interface javax.cache.Cache<K,V>
      Throws:
      UnsupportedOperationException - always
    • registerCacheEntryListener

      public void registerCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> config)
      Register a cache entry listener configuration.

      Note this method only supports registering a single CacheEntryCreatedListener instance. The configuration will be shared between this instance and the delegate Cache.

      Specified by:
      registerCacheEntryListener in interface javax.cache.Cache<K,V>
    • remove

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

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

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

      public void removeAll(Set<? extends K> keys)
      Specified by:
      removeAll 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>
    • replace

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

      public <T> T unwrap(Class<T> arg0)
      Unwrap an object.
      Specified by:
      unwrap in interface javax.cache.Cache<K,V>
      Throws:
      UnsupportedOperationException - always