Skip to content

Commit 69bc21f

Browse files
committed
Added ability to prime errors in the cache
1 parent e14b4d4 commit 69bc21f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/main/java/io/engagingspaces/vertx/dataloader/DataLoader.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ public DataLoader<K, V> prime(K key, V value) {
188188
return this;
189189
}
190190

191+
/**
192+
* Primes the cache with the given key and error.
193+
*
194+
* @param key the key
195+
* @param error the exception to prime instead of a value
196+
* @return the data loader for fluent coding
197+
*/
198+
public DataLoader<K, V> prime(K key, Exception error) {
199+
Object cacheKey = getCacheKey(key);
200+
if (!futureCache.containsKey(cacheKey)) {
201+
futureCache.set(cacheKey, Future.failedFuture(error));
202+
}
203+
return this;
204+
}
205+
191206
/**
192207
* Gets the object that is used in the internal cache map as key, by applying the cache key function to
193208
* the provided key.

0 commit comments

Comments
 (0)