Skip to content

DataLoader as an interface - 3.x branch #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ then you will not want to cache data meant for user A to then later give it user
The scope of your `DataLoader` instances is important. You might want to create them per web request to ensure data is only cached within that
web request and no more.

If your data can be shared across web requests then you might want to scope your data loaders so they survive longer than the web request say.
If your data can be shared across web requests then use a custom cache to keep values in a common place. You should however aim
to keep the data loader instances per web request because they are stateful components that contain promises (with context)
that are likely share the same affinity as the web request.

## Custom caches

Expand Down Expand Up @@ -461,7 +463,7 @@ repositories {
}

dependencies {
compile 'com.graphql-java:java-dataloader: 2.2.3'
compile 'com.graphql-java:java-dataloader: 3.0.0'
}
```

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dataloader/BatchLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.dataloader;

import org.dataloader.annotations.PublicSpi;

import java.util.List;
import java.util.concurrent.CompletionStage;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dataloader/BatchLoaderContextProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dataloader;

import org.dataloader.annotations.PublicSpi;

/**
* A BatchLoaderContextProvider is used by the {@link org.dataloader.DataLoader} code to
* provide overall calling context to the {@link org.dataloader.BatchLoader} call. A common use
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/dataloader/BatchLoaderEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.dataloader;

import org.dataloader.annotations.PublicApi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;

/**
* This object is passed to a batch loader as calling context. It could contain security credentials
Expand Down Expand Up @@ -78,8 +81,8 @@ public Builder context(Object context) {
}

public <K> Builder keyContexts(List<K> keys, List<Object> keyContexts) {
nonNull(keys);
nonNull(keyContexts);
requireNonNull(keys);
requireNonNull(keyContexts);

Map<Object, Object> map = new HashMap<>();
List<Object> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dataloader;

import org.dataloader.annotations.PublicSpi;

/**
* A BatchLoaderEnvironmentProvider is used by the {@link org.dataloader.DataLoader} code to
* provide {@link org.dataloader.BatchLoaderEnvironment} calling context to
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dataloader/BatchLoaderWithContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dataloader;

import org.dataloader.annotations.PublicSpi;

import java.util.List;
import java.util.concurrent.CompletionStage;

Expand Down
11 changes: 2 additions & 9 deletions src/main/java/org/dataloader/CacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.dataloader;

import org.dataloader.annotations.PublicSpi;
import org.dataloader.impl.DefaultCacheMap;

import java.util.concurrent.CompletableFuture;

/**
* Cache map interface for data loaders that use caching.
* <p>
Expand All @@ -31,7 +30,6 @@
*
* @param <U> type parameter indicating the type of the cache keys
* @param <V> type parameter indicating the type of the data that is cached
*
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
*/
Expand All @@ -43,18 +41,16 @@ public interface CacheMap<U, V> {
*
* @param <U> type parameter indicating the type of the cache keys
* @param <V> type parameter indicating the type of the data that is cached
*
* @return the cache map
*/
static <U, V> CacheMap<U, CompletableFuture<V>> simpleMap() {
static <U, V> CacheMap<U, V> simpleMap() {
return new DefaultCacheMap<>();
}

/**
* Checks whether the specified key is contained in the cach map.
*
* @param key the key to check
*
* @return {@code true} if the cache contains the key, {@code false} otherwise
*/
boolean containsKey(U key);
Expand All @@ -66,7 +62,6 @@ static <U, V> CacheMap<U, CompletableFuture<V>> simpleMap() {
* so be sure to check {@link CacheMap#containsKey(Object)} first.
*
* @param key the key to retrieve
*
* @return the cached value, or {@code null} if not found (depends on cache implementation)
*/
V get(U key);
Expand All @@ -76,7 +71,6 @@ static <U, V> CacheMap<U, CompletableFuture<V>> simpleMap() {
*
* @param key the key to cache
* @param value the value to cache
*
* @return the cache map for fluent coding
*/
CacheMap<U, V> set(U key, V value);
Expand All @@ -85,7 +79,6 @@ static <U, V> CacheMap<U, CompletableFuture<V>> simpleMap() {
* Deletes the entry with the specified key from the cache map, if it exists.
*
* @param key the key to delete
*
* @return the cache map for fluent coding
*/
CacheMap<U, V> delete(U key);
Expand Down
Loading