Skip to content

Commit efb3c9d

Browse files
authored
Merge pull request #100 from kelseyfrancis/prime-future
Allow priming with future values
2 parents 8836d7e + 58da8fb commit efb3c9d

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/main/java/org/dataloader/DataLoader.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -685,21 +685,15 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
685685
/**
686686
* Primes the cache with the given key and value. Note this will only prime the future cache
687687
* and not the value store. Use {@link ValueCache#set(Object, Object)} if you want
688-
* o prime it with values before use
688+
* to prime it with values before use
689689
*
690690
* @param key the key
691691
* @param value the value
692692
*
693693
* @return the data loader for fluent coding
694694
*/
695695
public DataLoader<K, V> prime(K key, V value) {
696-
Object cacheKey = getCacheKey(key);
697-
synchronized (this) {
698-
if (!futureCache.containsKey(cacheKey)) {
699-
futureCache.set(cacheKey, CompletableFuture.completedFuture(value));
700-
}
701-
}
702-
return this;
696+
return prime(key, CompletableFuture.completedFuture(value));
703697
}
704698

705699
/**
@@ -711,9 +705,25 @@ public DataLoader<K, V> prime(K key, V value) {
711705
* @return the data loader for fluent coding
712706
*/
713707
public DataLoader<K, V> prime(K key, Exception error) {
708+
return prime(key, CompletableFutureKit.failedFuture(error));
709+
}
710+
711+
/**
712+
* Primes the cache with the given key and value. Note this will only prime the future cache
713+
* and not the value store. Use {@link ValueCache#set(Object, Object)} if you want
714+
* to prime it with values before use
715+
*
716+
* @param key the key
717+
* @param value the value
718+
*
719+
* @return the data loader for fluent coding
720+
*/
721+
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
714722
Object cacheKey = getCacheKey(key);
715-
if (!futureCache.containsKey(cacheKey)) {
716-
futureCache.set(cacheKey, CompletableFutureKit.failedFuture(error));
723+
synchronized (this) {
724+
if (!futureCache.containsKey(cacheKey)) {
725+
futureCache.set(cacheKey, value);
726+
}
717727
}
718728
return this;
719729
}

src/test/java/org/dataloader/DataLoaderTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,24 @@ public void should_Allow_to_forcefully_prime_the_cache() throws ExecutionExcepti
362362
assertThat(loadCalls, equalTo(singletonList(singletonList("B"))));
363363
}
364364

365+
@Test
366+
public void should_Allow_priming_the_cache_with_a_future() throws ExecutionException, InterruptedException {
367+
List<Collection<String>> loadCalls = new ArrayList<>();
368+
DataLoader<String, String> identityLoader = idLoader(new DataLoaderOptions(), loadCalls);
369+
370+
DataLoader<String, String> dlFluency = identityLoader.prime("A", CompletableFuture.completedFuture("A"));
371+
assertThat(dlFluency, equalTo(identityLoader));
372+
373+
CompletableFuture<String> future1 = identityLoader.load("A");
374+
CompletableFuture<String> future2 = identityLoader.load("B");
375+
identityLoader.dispatch();
376+
377+
await().until(() -> future1.isDone() && future2.isDone());
378+
assertThat(future1.get(), equalTo("A"));
379+
assertThat(future2.get(), equalTo("B"));
380+
assertThat(loadCalls, equalTo(singletonList(singletonList("B"))));
381+
}
382+
365383
@Test
366384
public void should_not_Cache_failed_fetches_on_complete_failure() {
367385
List<Collection<Integer>> loadCalls = new ArrayList<>();

0 commit comments

Comments
 (0)