This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from yandooo/graphql-spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #964 from bsara/value-cache-helper
feat: added SpringValueCache for easy creation of data loader ValueCache objects that utilize Spring Caches under the hood
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...ckstart-spring-support/src/main/java/graphql/kickstart/spring/cache/SpringValueCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package graphql.kickstart.spring.cache; | ||
|
||
import static java.util.concurrent.CompletableFuture.runAsync; | ||
import static java.util.concurrent.CompletableFuture.supplyAsync; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
import lombok.RequiredArgsConstructor; | ||
import org.dataloader.ValueCache; | ||
import org.springframework.cache.Cache; | ||
import org.springframework.cache.Cache.ValueWrapper; | ||
|
||
/** | ||
* A {@link ValueCache} which uses a Spring {@link Cache} for caching. | ||
* | ||
* @see <a | ||
* href="https://www.graphql-java.com/documentation/batching/#per-request-data-loaders">GraphQL Java | ||
* docs</a> | ||
*/ | ||
@RequiredArgsConstructor | ||
public class SpringValueCache<K, V> implements ValueCache<K, V> { | ||
|
||
private final Cache cache; | ||
private Function<K, ?> keyTransformer; | ||
|
||
@Override | ||
public CompletableFuture<V> get(K key) { | ||
return supplyAsync(() -> { | ||
Object finalKey = this.getKey(key); | ||
ValueWrapper valueWrapper = this.cache.get(finalKey); | ||
|
||
if (valueWrapper == null) { | ||
throw new CacheEntryNotFoundException(this.cache.getName(), finalKey); | ||
} | ||
|
||
return (V) valueWrapper.get(); | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<V> set(K key, V value) { | ||
return supplyAsync(() -> { | ||
this.cache.put(this.getKey(key), value); | ||
return value; | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> delete(K key) { | ||
return runAsync(() -> this.cache.evictIfPresent(this.getKey(key))); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Void> clear() { | ||
return runAsync(this.cache::invalidate); | ||
} | ||
|
||
public <KFinal> SpringValueCache<K, V> setKeyTransformer(Function<K, KFinal> transformer) { | ||
this.keyTransformer = transformer; | ||
return this; | ||
} | ||
|
||
private Object getKey(K key) { | ||
return ( | ||
this.keyTransformer == null | ||
? key | ||
: this.keyTransformer.apply(key) | ||
); | ||
} | ||
|
||
public static class CacheEntryNotFoundException extends RuntimeException { | ||
public CacheEntryNotFoundException(String cacheName, Object key) { | ||
super("Entry could not be found in cache named \"" + cacheName + "\" for key: " + key); | ||
} | ||
} | ||
} |