11package org .dataloader ;
22
33import org .dataloader .annotations .PublicSpi ;
4+ import org .dataloader .impl .CompletableFutureKit ;
45import org .dataloader .impl .NoOpValueCache ;
56
7+ import java .util .ArrayList ;
8+ import java .util .List ;
69import java .util .concurrent .CompletableFuture ;
710
811/**
@@ -55,8 +58,6 @@ static <K, V> ValueCache<K, V> defaultValueCache() {
5558 * and not null because null is a valid cacheable value. An exceptionally completed future will cause {@link DataLoader} to load the key via batch loading
5659 * instead.
5760 * <p>
58- * NOTE: Your implementation MUST not throw exceptions, rather it should return a CompletableFuture that has completed exceptionally. Failure
59- * to do this may cause the {@link DataLoader} code to not run properly.
6061 *
6162 * @param key the key to retrieve
6263 *
@@ -66,10 +67,41 @@ static <K, V> ValueCache<K, V> defaultValueCache() {
6667 CompletableFuture <V > get (K key );
6768
6869 /**
69- * Stores the value with the specified key, or updates it if the key already exists.
70+ * Gets the specified key from the value cache. If the key is not present, then the returned {@link Try} will be a failed one
71+ * other wise it has the cached value. This is preferred over the {@link #get(Object)} method.
7072 * <p>
71- * NOTE: Your implementation MUST not throw exceptions, rather it should return a CompletableFuture that has completed exceptionally. Failure
72- * to do this may cause the {@link DataLoader} code to not run properly.
73+ *
74+ * @param key the key to retrieve
75+ *
76+ * @return a future containing the {@link Try} cached value (which maybe null) or a failed {@link Try} if the key does
77+ * not exist in the cache.
78+ */
79+ default CompletableFuture <Try <V >> getValue (K key ) {
80+ return Try .tryFuture (get (key ));
81+ }
82+
83+ /**
84+ * Gets the specified keys from the value cache, in a batch call. If your underlying cache cant do batch caching retrieval
85+ * then do not implement this method and it will delegate back to {@link #getValue(Object)} for you
86+ * <p>
87+ * You MUST return a List that is the same size as the keys passed in. The code will assert if you do not.
88+ *
89+ * @param keys the list of keys to get cached values for.
90+ *
91+ * @return a future containing a list of {@link Try} cached values (which maybe {@link Try#succeeded(Object)} or a failed {@link Try}
92+ * per key if they do not exist in the cache.
93+ */
94+ default CompletableFuture <List <Try <V >>> getValues (List <K > keys ) {
95+ List <CompletableFuture <Try <V >>> cacheLookups = new ArrayList <>();
96+ for (K key : keys ) {
97+ CompletableFuture <Try <V >> cacheTry = getValue (key );
98+ cacheLookups .add (cacheTry );
99+ }
100+ return CompletableFutureKit .allOf (cacheLookups );
101+ }
102+
103+ /**
104+ * Stores the value with the specified key, or updates it if the key already exists.
73105 *
74106 * @param key the key to store
75107 * @param value the value to store
@@ -78,6 +110,27 @@ static <K, V> ValueCache<K, V> defaultValueCache() {
78110 */
79111 CompletableFuture <V > set (K key , V value );
80112
113+ /**
114+ * Stores the value with the specified keys, or updates it if the keys if they already exist. If your underlying cache cant do batch caching setting
115+ * then do not implement this method and it will delegate back to {@link #set(Object, Object)} for you
116+ *
117+ * @param keys the keys to store
118+ * @param values the values to store
119+ *
120+ * @return a future containing the stored values for fluent composition
121+ */
122+ default CompletableFuture <List <V >> setValues (List <K > keys , List <V > values ) {
123+ List <CompletableFuture <V >> cacheSets = new ArrayList <>();
124+ for (int i = 0 ; i < keys .size (); i ++) {
125+ K k = keys .get (i );
126+ V v = values .get (i );
127+ CompletableFuture <V > setCall = set (k , v );
128+ CompletableFuture <V > set = Try .tryFuture (setCall ).thenApply (ignored -> v );
129+ cacheSets .add (set );
130+ }
131+ return CompletableFutureKit .allOf (cacheSets );
132+ }
133+
81134 /**
82135 * Deletes the entry with the specified key from the value cache, if it exists.
83136 * <p>
0 commit comments