3030import java .util .List ;
3131import java .util .Optional ;
3232import java .util .concurrent .CompletableFuture ;
33+ import java .util .function .BiConsumer ;
3334
3435import static org .dataloader .impl .Assertions .nonNull ;
3536
6465public class DataLoader <K , V > {
6566
6667 private final DataLoaderHelper <K , V > helper ;
67- private final CacheMap <Object , CompletableFuture <V >> futureCache ;
6868 private final StatisticsCollector stats ;
69+ private final CacheMap <Object , V > futureCache ;
70+ private final CachedValueStore <Object , V > cachedValueStore ;
6971
7072 /**
7173 * Creates new DataLoader with the specified batch loader function and default options
@@ -414,18 +416,23 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
414416 DataLoader (Object batchLoadFunction , DataLoaderOptions options , Clock clock ) {
415417 DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions () : options ;
416418 this .futureCache = determineCacheMap (loaderOptions );
419+ this .cachedValueStore = determineCacheStore (loaderOptions );
417420 // order of keys matter in data loader
418421 this .stats = nonNull (loaderOptions .getStatisticsCollector ());
419422
420- this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .stats , clock );
423+ this .helper = new DataLoaderHelper <>(this , batchLoadFunction , loaderOptions , this .futureCache , this .cachedValueStore , this . stats , clock );
421424 }
422425
423426
424427 @ SuppressWarnings ("unchecked" )
425- private CacheMap <Object , CompletableFuture < V > > determineCacheMap (DataLoaderOptions loaderOptions ) {
426- return loaderOptions . cacheMap (). isPresent () ? ( CacheMap <Object , CompletableFuture < V >> ) loaderOptions .cacheMap ().get () : CacheMap . simpleMap ( );
428+ private CacheMap <Object , V > determineCacheMap (DataLoaderOptions loaderOptions ) {
429+ return ( CacheMap <Object , V > ) loaderOptions .cacheMap ().orElseGet ( CacheMap :: simpleMap );
427430 }
428431
432+ @ SuppressWarnings ("unchecked" )
433+ private CachedValueStore <Object , V > determineCacheStore (DataLoaderOptions loaderOptions ) {
434+ return (CachedValueStore <Object , V >) loaderOptions .cachedValueStore ().orElseGet (CachedValueStore ::defaultStore );
435+ }
429436
430437 /**
431438 * This returns the last instant the data loader was dispatched. When the data loader is created this value is set to now.
@@ -628,9 +635,24 @@ public int dispatchDepth() {
628635 * @return the data loader for fluent coding
629636 */
630637 public DataLoader <K , V > clear (K key ) {
638+ return clear (key , (v , e ) -> {
639+ });
640+ }
641+
642+ /**
643+ * Clears the future with the specified key from the cache remote value store, if caching is enabled
644+ * and a remote store is set, so it will be re-fetched and stored on the next load request.
645+ *
646+ * @param key the key to remove
647+ * @param handler a handler that will be called after the async remote clear completes
648+ *
649+ * @return the data loader for fluent coding
650+ */
651+ public DataLoader <K , V > clear (K key , BiConsumer <Void , Throwable > handler ) {
631652 Object cacheKey = getCacheKey (key );
632653 synchronized (this ) {
633654 futureCache .delete (cacheKey );
655+ cachedValueStore .delete (key ).whenComplete (handler );
634656 }
635657 return this ;
636658 }
@@ -641,14 +663,29 @@ public DataLoader<K, V> clear(K key) {
641663 * @return the data loader for fluent coding
642664 */
643665 public DataLoader <K , V > clearAll () {
666+ return clearAll ((v , e ) -> {
667+ });
668+ }
669+
670+ /**
671+ * Clears the entire cache map of the loader, and of the cached value store.
672+ *
673+ * @param handler a handler that will be called after the async remote clear all completes
674+ *
675+ * @return the data loader for fluent coding
676+ */
677+ public DataLoader <K , V > clearAll (BiConsumer <Void , Throwable > handler ) {
644678 synchronized (this ) {
645679 futureCache .clear ();
680+ cachedValueStore .clear ().whenComplete (handler );
646681 }
647682 return this ;
648683 }
649684
650685 /**
651- * Primes the cache with the given key and value.
686+ * Primes the cache with the given key and value. Note this will only prime the future cache
687+ * and not the value store. Use {@link CachedValueStore#set(Object, Object)} if you want
688+ * o prime it with values before use
652689 *
653690 * @param key the key
654691 * @param value the value
0 commit comments