@@ -176,38 +176,37 @@ private CacheMap<Object, CompletableFuture<V>> determineCacheMap(DataLoaderOptio
176
176
* @return the future of the value
177
177
*/
178
178
public CompletableFuture <V > load (K key ) {
179
- Object cacheKey = getCacheKey (nonNull (key ));
180
- stats .incrementLoadCount ();
179
+ synchronized (this ) {
180
+ Object cacheKey = getCacheKey (nonNull (key ));
181
+ stats .incrementLoadCount ();
181
182
182
- if (loaderOptions .cachingEnabled ()) {
183
- synchronized (futureCache ) {
183
+ boolean batchingEnabled = loaderOptions .batchingEnabled ();
184
+ boolean cachingEnabled = loaderOptions .cachingEnabled ();
185
+
186
+ if (cachingEnabled ) {
184
187
if (futureCache .containsKey (cacheKey )) {
185
188
stats .incrementCacheHitCount ();
186
189
return futureCache .get (cacheKey );
187
190
}
188
191
}
189
- }
190
192
191
- CompletableFuture <V > future = new CompletableFuture <>();
192
- if (loaderOptions .batchingEnabled ()) {
193
- synchronized (loaderQueue ) {
193
+ CompletableFuture <V > future = new CompletableFuture <>();
194
+ if (batchingEnabled ) {
194
195
loaderQueue .add (new SimpleImmutableEntry <>(key , future ));
196
+ } else {
197
+ stats .incrementBatchLoadCountBy (1 );
198
+ // immediate execution of batch function
199
+ CompletableFuture <List <V >> batchedLoad = batchLoadFunction
200
+ .load (singletonList (key ))
201
+ .toCompletableFuture ();
202
+ future = batchedLoad
203
+ .thenApply (list -> list .get (0 ));
195
204
}
196
- } else {
197
- stats .incrementBatchLoadCountBy (1 );
198
- // immediate execution of batch function
199
- CompletableFuture <List <V >> batchedLoad = batchLoadFunction
200
- .load (singletonList (key ))
201
- .toCompletableFuture ();
202
- future = batchedLoad
203
- .thenApply (list -> list .get (0 ));
204
- }
205
- if (loaderOptions .cachingEnabled ()) {
206
- synchronized (futureCache ) {
205
+ if (cachingEnabled ) {
207
206
futureCache .set (cacheKey , future );
208
207
}
208
+ return future ;
209
209
}
210
- return future ;
211
210
}
212
211
213
212
/**
@@ -223,8 +222,7 @@ public CompletableFuture<V> load(K key) {
223
222
* @return the composite future of the list of values
224
223
*/
225
224
public CompletableFuture <List <V >> loadMany (List <K > keys ) {
226
- synchronized (loaderQueue ) {
227
-
225
+ synchronized (this ) {
228
226
List <CompletableFuture <V >> collect = keys .stream ()
229
227
.map (this ::load )
230
228
.collect (Collectors .toList ());
@@ -241,18 +239,19 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
241
239
* @return the promise of the queued load requests
242
240
*/
243
241
public CompletableFuture <List <V >> dispatch () {
242
+ boolean batchingEnabled = loaderOptions .batchingEnabled ();
244
243
//
245
244
// we copy the pre-loaded set of futures ready for dispatch
246
245
final List <K > keys = new ArrayList <>();
247
246
final List <CompletableFuture <V >> queuedFutures = new ArrayList <>();
248
- synchronized (loaderQueue ) {
247
+ synchronized (this ) {
249
248
loaderQueue .forEach (entry -> {
250
249
keys .add (entry .getKey ());
251
250
queuedFutures .add (entry .getValue ());
252
251
});
253
252
loaderQueue .clear ();
254
253
}
255
- if (!loaderOptions . batchingEnabled () || keys .size () == 0 ) {
254
+ if (!batchingEnabled || keys .size () == 0 ) {
256
255
return CompletableFuture .completedFuture (emptyList ());
257
256
}
258
257
//
@@ -375,7 +374,7 @@ public List<V> dispatchAndJoin() {
375
374
* @return the depth of the batched key loads that need to be dispatched
376
375
*/
377
376
public int dispatchDepth () {
378
- synchronized (loaderQueue ) {
377
+ synchronized (this ) {
379
378
return loaderQueue .size ();
380
379
}
381
380
}
@@ -391,7 +390,7 @@ public int dispatchDepth() {
391
390
*/
392
391
public DataLoader <K , V > clear (K key ) {
393
392
Object cacheKey = getCacheKey (key );
394
- synchronized (futureCache ) {
393
+ synchronized (this ) {
395
394
futureCache .delete (cacheKey );
396
395
}
397
396
return this ;
@@ -403,7 +402,7 @@ public DataLoader<K, V> clear(K key) {
403
402
* @return the data loader for fluent coding
404
403
*/
405
404
public DataLoader <K , V > clearAll () {
406
- synchronized (futureCache ) {
405
+ synchronized (this ) {
407
406
futureCache .clear ();
408
407
}
409
408
return this ;
@@ -419,7 +418,7 @@ public DataLoader<K, V> clearAll() {
419
418
*/
420
419
public DataLoader <K , V > prime (K key , V value ) {
421
420
Object cacheKey = getCacheKey (key );
422
- synchronized (futureCache ) {
421
+ synchronized (this ) {
423
422
if (!futureCache .containsKey (cacheKey )) {
424
423
futureCache .set (cacheKey , CompletableFuture .completedFuture (value ));
425
424
}
0 commit comments