@@ -362,21 +362,20 @@ CompletionStage<List<V>> invokeLoader(List<K> keys, List<Object> keyContexts, bo
362
362
// and then fill in their values
363
363
//
364
364
CompletionStage <List <V >> batchLoad = invokeLoader (cacheMissedKeys , cacheMissedContexts );
365
- CompletionStage < CacheMissedData < K , V >> assembledValues = batchLoad .thenApply ( batchedValues -> {
366
- assertResultSize (cacheMissedKeys , batchedValues );
365
+ return batchLoad .thenCompose ( missedValues -> {
366
+ assertResultSize (cacheMissedKeys , missedValues );
367
367
368
- for (int i = 0 ; i < batchedValues .size (); i ++) {
368
+ for (int i = 0 ; i < missedValues .size (); i ++) {
369
369
K missedKey = cacheMissedKeys .get (i );
370
- V v = batchedValues .get (i );
370
+ V v = missedValues .get (i );
371
371
valuesInKeyOrder .put (missedKey , v );
372
372
}
373
-
374
- return new CacheMissedData <>(cacheMissedKeys , batchedValues , new ArrayList <>(valuesInKeyOrder .values ()));
373
+ List <V > assembledValues = new ArrayList <>(valuesInKeyOrder .values ());
374
+ //
375
+ // fire off a call to the ValueCache to allow it to set values into the
376
+ // cache now that we have them
377
+ return setToValueCache (assembledValues , cacheMissedKeys , missedValues );
375
378
});
376
- //
377
- // fire off a call to the ValueCache to allow it to set values into the
378
- // cache now that we have them
379
- return setToValueCache (assembledValues );
380
379
}
381
380
});
382
381
}
@@ -453,40 +452,24 @@ private CompletableFuture<List<Try<V>>> getFromValueCache(List<K> keys) {
453
452
}
454
453
}
455
454
456
- static class CacheMissedData <K , V > {
457
- final List <K > missedKeys ;
458
- final List <V > missedValues ;
459
- final List <V > assembledValues ;
460
-
461
- CacheMissedData (List <K > missedKeys , List <V > missedValues , List <V > assembledValues ) {
462
- this .missedKeys = missedKeys ;
463
- this .missedValues = missedValues ;
464
- this .assembledValues = assembledValues ;
465
- }
466
- }
467
-
468
- private CompletionStage <List <V >> setToValueCache (CompletionStage <CacheMissedData <K , V >> assembledValues ) {
455
+ private CompletionStage <List <V >> setToValueCache (List <V > assembledValues , List <K > missedKeys , List <V > missedValues ) {
469
456
try {
470
457
boolean completeValueAfterCacheSet = loaderOptions .getValueCacheOptions ().isCompleteValueAfterCacheSet ();
471
458
if (completeValueAfterCacheSet ) {
472
- return assembledValues . thenCompose ( cacheMissedData -> nonNull (valueCache
473
- .setValues (cacheMissedData . missedKeys , cacheMissedData . missedValues ), () -> "Your ValueCache.setValues function MUST return a non null promise" )
459
+ return nonNull (valueCache
460
+ .setValues (missedKeys , missedValues ), () -> "Your ValueCache.setValues function MUST return a non null promise" )
474
461
// we dont trust the set cache to give us the values back - we have them - lets use them
475
462
// if the cache set fails - then they wont be in cache and maybe next time they will
476
- .handle ((ignored , setExIgnored ) -> cacheMissedData . assembledValues ) );
463
+ .handle ((ignored , setExIgnored ) -> assembledValues );
477
464
} else {
478
- return assembledValues .thenApply (cacheMissedData -> {
479
- // no one is waiting for the set to happen here so if its truly async
480
- // it will happen eventually but no result will be dependant on it
481
- valueCache .setValues (cacheMissedData .missedKeys , cacheMissedData .missedValues );
482
- return cacheMissedData .assembledValues ;
483
- });
465
+ // no one is waiting for the set to happen here so if its truly async
466
+ // it will happen eventually but no result will be dependant on it
467
+ valueCache .setValues (missedKeys , missedValues );
484
468
}
485
469
} catch (RuntimeException ignored ) {
486
470
// if we cant set values back into the cache - so be it - this must be a faulty
487
471
// ValueCache implementation
488
- return assembledValues .thenApply (cacheMissedData -> cacheMissedData .assembledValues );
489
472
}
473
+ return CompletableFuture .completedFuture (assembledValues );
490
474
}
491
-
492
475
}
0 commit comments