Class 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 a AsyncBulkCacheLoader 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 with CoalescingBulkLoaderSupport. If in doubt check the test cases.

    Author:
    Jens Wilke
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.cache2k.io.AsyncBulkCacheLoader

        org.cache2k.io.AsyncBulkCacheLoader.BulkCallback<K extends Object,​V extends Object>, org.cache2k.io.AsyncBulkCacheLoader.BulkLoadContext<K extends Object,​V extends Object>
      • Nested classes/interfaces inherited from interface org.cache2k.io.AsyncCacheLoader

        org.cache2k.io.AsyncCacheLoader.Callback<V extends Object>, org.cache2k.io.AsyncCacheLoader.Context<K extends Object,​V extends Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      CoalescingBulkLoader​(org.cache2k.io.AsyncBulkCacheLoader<K,​V> forwardingLoader, long maxDelayMillis, int maxBatchSize, boolean refreshOnly)
      Constructor using the default time reference TimeReference.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.
    • Constructor Detail

      • 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 via TimeReference.toMillis(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)
        Specified by:
        loadAll in interface org.cache2k.io.AsyncBulkCacheLoader<K,​V>
      • 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 due
        onlyWhenFull - true for queue spill, double check within lock
        Returns:
        true if there might be more to process
      • getQueueSize

        public long getQueueSize()