30
30
import java .util .List ;
31
31
import java .util .Optional ;
32
32
import java .util .concurrent .CompletableFuture ;
33
+ import java .util .function .BiConsumer ;
33
34
34
35
import static org .dataloader .impl .Assertions .nonNull ;
35
36
64
65
public class DataLoader <K , V > {
65
66
66
67
private final DataLoaderHelper <K , V > helper ;
67
- private final CacheMap <Object , CompletableFuture <V >> futureCache ;
68
68
private final StatisticsCollector stats ;
69
+ private final CacheMap <Object , V > futureCache ;
70
+ private final CachedValueStore <Object , V > cachedValueStore ;
69
71
70
72
/**
71
73
* Creates new DataLoader with the specified batch loader function and default options
@@ -414,18 +416,23 @@ public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options
414
416
DataLoader (Object batchLoadFunction , DataLoaderOptions options , Clock clock ) {
415
417
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions () : options ;
416
418
this .futureCache = determineCacheMap (loaderOptions );
419
+ this .cachedValueStore = determineCacheStore (loaderOptions );
417
420
// order of keys matter in data loader
418
421
this .stats = nonNull (loaderOptions .getStatisticsCollector ());
419
422
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 );
421
424
}
422
425
423
426
424
427
@ 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 );
427
430
}
428
431
432
+ @ SuppressWarnings ("unchecked" )
433
+ private CachedValueStore <Object , V > determineCacheStore (DataLoaderOptions loaderOptions ) {
434
+ return (CachedValueStore <Object , V >) loaderOptions .cachedValueStore ().orElseGet (CachedValueStore ::defaultStore );
435
+ }
429
436
430
437
/**
431
438
* 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() {
628
635
* @return the data loader for fluent coding
629
636
*/
630
637
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 ) {
631
652
Object cacheKey = getCacheKey (key );
632
653
synchronized (this ) {
633
654
futureCache .delete (cacheKey );
655
+ cachedValueStore .delete (key ).whenComplete (handler );
634
656
}
635
657
return this ;
636
658
}
@@ -641,14 +663,29 @@ public DataLoader<K, V> clear(K key) {
641
663
* @return the data loader for fluent coding
642
664
*/
643
665
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 ) {
644
678
synchronized (this ) {
645
679
futureCache .clear ();
680
+ cachedValueStore .clear ().whenComplete (handler );
646
681
}
647
682
return this ;
648
683
}
649
684
650
685
/**
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
652
689
*
653
690
* @param key the key
654
691
* @param value the value
0 commit comments