Skip to content

Generate cache key with loadContext. #78

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

Merged
merged 2 commits into from
Feb 26, 2021
Merged
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
13 changes: 13 additions & 0 deletions src/main/java/org/dataloader/CacheKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ public interface CacheKey<K> {
* @return the cache key
*/
Object getKey(K input);

/**
* Returns the cache key that is created from the provided input key and context.
*
* @param input the input key
*
* @param context the context
*
* @return the cache key
*/
default Object getKeyWithContext(K input, Object context) {
return getKey(input);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great PR - simple and powerful.

Can you put Javadoc on this method and I will merge it

Copy link
Contributor Author

@qzchenwl qzchenwl Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaDoc is added now. @bbakerman

}
15 changes: 14 additions & 1 deletion src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ CompletableFuture<V> load(K key, Object loadContext) {
boolean batchingEnabled = loaderOptions.batchingEnabled();
boolean cachingEnabled = loaderOptions.cachingEnabled();

Object cacheKey = cachingEnabled ? getCacheKey(nonNull(key)) : null;
Object cacheKey = null;
if (cachingEnabled) {
if (loadContext == null) {
cacheKey = getCacheKey(key);
} else {
cacheKey = getCacheKeyWithContext(key, loadContext);
}
}
stats.incrementLoadCount();

if (cachingEnabled) {
Expand Down Expand Up @@ -135,6 +142,12 @@ Object getCacheKey(K key) {
loaderOptions.cacheKeyFunction().get().getKey(key) : key;
}

@SuppressWarnings("unchecked")
Object getCacheKeyWithContext(K key, Object context) {
return loaderOptions.cacheKeyFunction().isPresent() ?
loaderOptions.cacheKeyFunction().get().getKeyWithContext(key, context): key;
}

DispatchResult<V> dispatch() {
boolean batchingEnabled = loaderOptions.batchingEnabled();
//
Expand Down