Skip to content

Commit 712c328

Browse files
committed
Fixed bug in caching, improved generics handling
1 parent 97a77ab commit 712c328

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
* Function that is invoked on input keys of type {@code K} to derive keys that are required by the {@link CacheMap}
2121
* implementation.
2222
*
23+
* @param <K> type parameter indicating the type of the input key
2324
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
2425
*/
2526
@FunctionalInterface
26-
public interface CacheKey {
27+
public interface CacheKey<K> {
2728

2829
/**
2930
* Returns the cache key that is created from the provided input key.
3031
*
3132
* @param input the input key
32-
* @param <K> type parameter indicating the type of the input key
33-
* @param <U> type parameter indicating the type of the cache key that is returned
3433
* @return the cache key
3534
*/
36-
<K, U> U getKey(K input);
35+
Object getKey(K input);
3736
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
public class DataLoader<K, V> {
5050

5151
private final BatchLoader<K> batchLoadFunction;
52-
private final DataLoaderOptions<K, V> loaderOptions;
52+
private final DataLoaderOptions loaderOptions;
5353
private final CacheMap<Object, Future<V>> futureCache;
5454
private final LinkedHashMap<K, Future<V>> loaderQueue;
5555

@@ -69,10 +69,10 @@ public DataLoader(BatchLoader<K> batchLoadFunction) {
6969
* @param options the batch load options
7070
*/
7171
@SuppressWarnings("unchecked")
72-
public DataLoader(BatchLoader<K> batchLoadFunction, DataLoaderOptions<K, V> options) {
72+
public DataLoader(BatchLoader<K> batchLoadFunction, DataLoaderOptions options) {
7373
Objects.requireNonNull(batchLoadFunction, "Batch load function cannot be null");
7474
this.batchLoadFunction = batchLoadFunction;
75-
this.loaderOptions = options == null ? new DataLoaderOptions<>() : options;
75+
this.loaderOptions = options == null ? new DataLoaderOptions() : options;
7676
this.futureCache = loaderOptions.cacheMap().isPresent() ? (CacheMap<Object, Future<V>>) loaderOptions.cacheMap().get() : CacheMap.simpleMap();
7777
this.loaderQueue = new LinkedHashMap<>();
7878
}
@@ -91,7 +91,7 @@ public Future<V> load(K key) {
9191
Objects.requireNonNull(key, "Key cannot be null");
9292
Object cacheKey = getCacheKey(key);
9393
if (loaderOptions.cachingEnabled() && futureCache.containsKey(cacheKey)) {
94-
return futureCache.get(key);
94+
return futureCache.get(cacheKey);
9595
}
9696

9797
Future<V> future = Future.future();
@@ -106,7 +106,7 @@ public Future<V> load(K key) {
106106
}
107107
}
108108
if (loaderOptions.cachingEnabled()) {
109-
futureCache.set(key, future);
109+
futureCache.set(cacheKey, future);
110110
}
111111
return future;
112112
}
@@ -213,6 +213,7 @@ public DataLoader<K, V> prime(K key, Exception error) {
213213
* @param key the input key
214214
* @return the cache key after the input is transformed with the cache key function
215215
*/
216+
@SuppressWarnings("unchecked")
216217
public Object getCacheKey(K key) {
217218
return loaderOptions.cacheKeyFunction().isPresent() ?
218219
loaderOptions.cacheKeyFunction().get().getKey(key) : key;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*
2828
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
2929
*/
30-
public class DataLoaderOptions<K, V> {
30+
public class DataLoaderOptions {
3131

3232
private boolean batchingEnabled;
3333
private boolean cachingEnabled;
3434
private CacheKey cacheKeyFunction;
35-
private CacheMap<K, Future<V>> cacheMap;
35+
private CacheMap cacheMap;
3636

3737
/**
3838
* Creates a new data loader options with default settings.
@@ -42,16 +42,16 @@ public DataLoaderOptions() {
4242
cachingEnabled = true;
4343
}
4444

45-
public static <K, V> DataLoaderOptions<K, V> create() {
46-
return new DataLoaderOptions<>();
45+
public static DataLoaderOptions create() {
46+
return new DataLoaderOptions();
4747
}
4848

4949
/**
5050
* Clones the provided data loader options.
5151
*
5252
* @param other the other options instance
5353
*/
54-
public DataLoaderOptions(DataLoaderOptions<K, V> other) {
54+
public DataLoaderOptions(DataLoaderOptions other) {
5555
Objects.requireNonNull(other, "Other data loader options cannot be null");
5656
this.batchingEnabled = other.batchingEnabled;
5757
this.cachingEnabled = other.cachingEnabled;
@@ -90,7 +90,7 @@ public boolean batchingEnabled() {
9090
* @param batchingEnabled {@code true} to enable batch loading, {@code false} otherwise
9191
* @return the data loader options for fluent coding
9292
*/
93-
public DataLoaderOptions<K, V> setBatchingEnabled(boolean batchingEnabled) {
93+
public DataLoaderOptions setBatchingEnabled(boolean batchingEnabled) {
9494
this.batchingEnabled = batchingEnabled;
9595
return this;
9696
}
@@ -110,7 +110,7 @@ public boolean cachingEnabled() {
110110
* @param cachingEnabled {@code true} to enable caching, {@code false} otherwise
111111
* @return the data loader options for fluent coding
112112
*/
113-
public DataLoaderOptions<K, V> setCachingEnabled(boolean cachingEnabled) {
113+
public DataLoaderOptions setCachingEnabled(boolean cachingEnabled) {
114114
this.cachingEnabled = cachingEnabled;
115115
return this;
116116
}
@@ -132,7 +132,7 @@ public Optional<CacheKey> cacheKeyFunction() {
132132
* @param cacheKeyFunction the cache key function to use
133133
* @return the data loader options for fluent coding
134134
*/
135-
public DataLoaderOptions<K, V> setCacheKeyFunction(CacheKey cacheKeyFunction) {
135+
public DataLoaderOptions setCacheKeyFunction(CacheKey cacheKeyFunction) {
136136
this.cacheKeyFunction = cacheKeyFunction;
137137
return this;
138138
}
@@ -144,7 +144,7 @@ public DataLoaderOptions<K, V> setCacheKeyFunction(CacheKey cacheKeyFunction) {
144144
*
145145
* @return an optional with the cache map instance, or empty
146146
*/
147-
public Optional<CacheMap<K, Future<V>>> cacheMap() {
147+
public Optional<CacheMap> cacheMap() {
148148
return Optional.ofNullable(cacheMap);
149149
}
150150

@@ -154,7 +154,7 @@ public Optional<CacheMap<K, Future<V>>> cacheMap() {
154154
* @param cacheMap the cache map instance
155155
* @return the data loader options for fluent coding
156156
*/
157-
public DataLoaderOptions setCacheMap(CacheMap<K, Future<V>> cacheMap) {
157+
public <K, V> DataLoaderOptions setCacheMap(CacheMap<K, Future<V>> cacheMap) {
158158
this.cacheMap = cacheMap;
159159
return this;
160160
}

0 commit comments

Comments
 (0)