Class BufferingDelegatingCache<K,V>
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterable<javax.cache.Cache.Entry<K,,V>> 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
-
Nested Class Summary
Nested classes/interfaces inherited from interface javax.cache.Cache
javax.cache.Cache.Entry<K,V> -
Constructor Summary
ConstructorsConstructorDescriptionBufferingDelegatingCache(javax.cache.Cache<K, V> delegate, int internalCapacity) Constructor.BufferingDelegatingCache(javax.cache.Cache<K, V> delegate, int internalCapacity, ConcurrentMap<K, V> internalStore) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclose()booleancontainsKey(K key) voidderegisterCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K, V> config) getAndRemove(K key) getAndReplace(K key, V value) javax.cache.CacheManagergetConfiguration(Class<C> clazz) intGet the configured internal capacity.intGet the number of elements currently stored internally.intGet the highest number of elements stored internally, since the last reset viaclear().getName()<T> TbooleanisClosed()iterator()Get an iterator over all cache elements.voidvoidAdd an element to the cache.voidbooleanputIfAbsent(K key, V value) Add an element to the cache only if its key does not already exist in the cache.voidregisterCacheEntryListener(javax.cache.configuration.CacheEntryListenerConfiguration<K, V> config) Register a cache entry listener configuration.booleanbooleanvoidvoidbooleanboolean<T> TUnwrap an object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BufferingDelegatingCache
Constructor.A
ConcurrentHashMapwill be created for the internal store.- Parameters:
delegate- the cache to delegate elements to once more thaninternalCapacityelemements are managed by the cacheinternalCapacity- 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 thaninternalCapacityelements are managed by the cacheinternalCapacity- the maximum number of elements to keep in the internal storeinternalStore- 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 viaclear().- Returns:
- the highest number of elements stored internally
- Since:
- 1.1
-
clear
public void clear() -
close
public void close() -
containsKey
-
deregisterCacheEntryListener
-
get
-
getAll
-
getAndPut
-
getAndRemove
-
getAndReplace
-
getCacheManager
public javax.cache.CacheManager getCacheManager() -
getConfiguration
-
getName
-
invoke
-
invokeAll
-
isClosed
public boolean isClosed() -
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. -
loadAll
-
put
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 delegateCache. -
putAll
-
putIfAbsent
Add an element to the cache only if its key does not already exist in the cache.- Specified by:
putIfAbsentin interfacejavax.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
CacheEntryCreatedListenerinstance. The configuration will be shared between this instance and the delegateCache. -
remove
-
remove
-
removeAll
public void removeAll() -
removeAll
-
replace
-
replace
-
unwrap
Unwrap an object.- Specified by:
unwrapin interfacejavax.cache.Cache<K,V> - Throws:
UnsupportedOperationException- always
-