Skip to content

Remove CacheMap#containsKey before #get #146

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
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
3 changes: 1 addition & 2 deletions src/main/java/org/dataloader/CacheMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ static <K, V> CacheMap<K, V> simpleMap() {
/**
* Gets the specified key from the cache map.
* <p>
* May throw an exception if the key does not exist, depending on the cache map implementation that is used,
* so be sure to check {@link CacheMap#containsKey(Object)} first.
* May throw an exception if the key does not exist, depending on the cache map implementation that is used.
*
* @param key the key to retrieve
*
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/org/dataloader/DataLoaderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ Optional<CompletableFuture<V>> getIfPresent(K key) {
boolean cachingEnabled = loaderOptions.cachingEnabled();
if (cachingEnabled) {
Object cacheKey = getCacheKey(nonNull(key));
if (futureCache.containsKey(cacheKey)) {
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key));
return Optional.of(futureCache.get(cacheKey));
try {
CompletableFuture<V> cacheValue = futureCache.get(cacheKey);
if (cacheValue != null) {
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key));
return Optional.of(cacheValue);
}
} catch (Exception ignored) {
}
}
}
Expand Down Expand Up @@ -307,10 +311,14 @@ private void possiblyClearCacheEntriesOnExceptions(List<K> keys) {
private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean batchingEnabled) {
final Object cacheKey = loadContext == null ? getCacheKey(key) : getCacheKeyWithContext(key, loadContext);

if (futureCache.containsKey(cacheKey)) {
// We already have a promise for this key, no need to check value cache or queue up load
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key, loadContext));
return futureCache.get(cacheKey);
try {
CompletableFuture<V> cacheValue = futureCache.get(cacheKey);
if (cacheValue != null) {
// We already have a promise for this key, no need to check value cache or queue up load
stats.incrementCacheHitCount(new IncrementCacheHitCountStatisticsContext<>(key, loadContext));
return cacheValue;
}
} catch (Exception ignored) {
}

CompletableFuture<V> loadCallFuture = queueOrInvokeLoader(key, loadContext, batchingEnabled, true);
Expand Down
Loading