Package org.cache2k.addon
Class CoalescingBulkLoader<K,V>
- java.lang.Object
-
- org.cache2k.addon.CoalescingBulkLoader<K,V>
-
- All Implemented Interfaces:
AutoCloseable
,org.cache2k.Customization
,org.cache2k.DataAware<K,V>
,org.cache2k.DataAwareCustomization<K,V>
,org.cache2k.io.AsyncBulkCacheLoader<K,V>
,org.cache2k.io.AsyncCacheLoader<K,V>
public class CoalescingBulkLoader<K,V> extends Object implements org.cache2k.io.AsyncBulkCacheLoader<K,V>, AutoCloseable
Wraps aAsyncBulkCacheLoader
and combines (single) load requests into bulk requests. Usually a bulk load takes place, when a bulk operation, e.g.Cache.getAll(java.lang.Iterable<? extends K>)
is issued. The cache core is producing no bulk requests take place for refresh, because every expiry is handled individually. This class coalesces requests into larger chunks.Parameters: You may specify how long requests are being delayed and a maximum of loads coalesced into one batch.
Coalescing by default only happens for refresh ahead requests. This is controlled via the parameter
refreshOnly
. Requests that are not refresh ahead are client issued and executed immediately, together with any pending refresh ahead requests.Usage: Either use the constructor
CoalescingBulkLoader(AsyncBulkCacheLoader, long, int, boolean)
and wrap a loader explicitly, or use the declarative configuration withCoalescingBulkLoaderSupport
. If in doubt check the test cases.- Author:
- Jens Wilke
-
-
Constructor Summary
Constructors Constructor Description CoalescingBulkLoader(org.cache2k.io.AsyncBulkCacheLoader<K,V> forwardingLoader, long maxDelayMillis, int maxBatchSize, boolean refreshOnly)
Constructor using the default time referenceTimeReference.DEFAULT
CoalescingBulkLoader(org.cache2k.io.AsyncBulkCacheLoader<K,V> forwardingLoader, org.cache2k.operation.TimeReference timeReference, long maxDelayMillis, int maxBatchSize, boolean refreshOnly)
Constructor using the specified time reference instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
Send all pending requests to the loader.boolean
forwardRequests(boolean timerEvent, boolean onlyWhenFull)
Gather requests up to max batch size and forward to the loaderlong
getQueueSize()
void
loadAll(Set<K> keys, org.cache2k.io.AsyncBulkCacheLoader.BulkLoadContext<K,V> context, org.cache2k.io.AsyncBulkCacheLoader.BulkCallback<K,V> callback)
-
-
-
Constructor Detail
-
CoalescingBulkLoader
public CoalescingBulkLoader(org.cache2k.io.AsyncBulkCacheLoader<K,V> forwardingLoader, long maxDelayMillis, int maxBatchSize, boolean refreshOnly)
Constructor using the default time referenceTimeReference.DEFAULT
- Parameters:
forwardingLoader
- requests are forwarded to this loadermaxDelayMillis
- seeCoalescingBulkLoaderConfig.Builder.maxDelay(long, TimeUnit)
maxBatchSize
- seeCoalescingBulkLoaderConfig.Builder.maxBatchSize(int)
refreshOnly
- seeCoalescingBulkLoaderConfig.Builder.refreshOnly(boolean)
-
CoalescingBulkLoader
public CoalescingBulkLoader(org.cache2k.io.AsyncBulkCacheLoader<K,V> forwardingLoader, org.cache2k.operation.TimeReference timeReference, long maxDelayMillis, int maxBatchSize, boolean refreshOnly)
Constructor using the specified time reference instance.- Parameters:
timeReference
- if the cache is using a different time reference, the instance is used to translate to milli seconds viaTimeReference.ticksToMillisCeiling(long)
-
-
Method Detail
-
loadAll
public void loadAll(Set<K> keys, org.cache2k.io.AsyncBulkCacheLoader.BulkLoadContext<K,V> context, org.cache2k.io.AsyncBulkCacheLoader.BulkCallback<K,V> callback)
-
flush
public void flush()
Send all pending requests to the loader. This is used for testing.
-
forwardRequests
public boolean forwardRequests(boolean timerEvent, boolean onlyWhenFull)
Gather requests up to max batch size and forward to the loader- Parameters:
timerEvent
- true for timer event, don't do anything if not dueonlyWhenFull
- true for queue spill, double check within lock- Returns:
- true if there might be more to process
-
getQueueSize
public long getQueueSize()
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
-