Skip to content

cache values and errors, not futures #81

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 2 commits into from
Closed

cache values and errors, not futures #81

wants to merge 2 commits into from

Conversation

craig-day
Copy link

@craig-day craig-day commented May 5, 2021

I believe this fixes #59 by explicitly caching values, not completed futures, and it implicitly fixes #45 because the signature becomes correct with this change.

I don't think there is a way to do this in a non-breaking fashion, but I still think it's worth doing for the next release. Since I'm in here breaking things, one question I have is why do we use Object everywhere for the cache key. Can we instead change the signature of CacheMap<K, V> to be CacheMap<V> and then have the functions look like get(CacheKey key) and set(CacheKey key, V value)?

On the breaking note, I changed the error state to use Throwable because it's more common and generic. It also plays nicer with converting to/from CompletableFuture.

I also switched the default implementation to use a ConcurrentHashMap which can help avoid some synchronized calls and eliminate the chance of colliding cache operations. In addition, I added a quality-of-life function, computeIfAbsent, but I'm happy to remove that from this PR if we think it's out of scope.

@craig-day
Copy link
Author

@bbakerman can I get some initial thoughts on this before I push further with testing and other code cleanup/refactor?

Comment on lines +566 to +570
if (!valueCache.containsKey(cacheKey)) {
valueCache.set(cacheKey, error);
}
Copy link
Author

Choose a reason for hiding this comment

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

The more I look at this, the more I'm not sure if it's right. Initially, I changed the CacheMap#get function to return a Try to allow priming the cache with an error, but I'm wondering if that is right. Maybe we just keep an independent cache of errors to support priming failures, but not let the CacheMap worry about that. That makes a lot more sense to me than caching errors, which are often times much more temporary than a cache would be.

@craig-day
Copy link
Author

I see the problems now around caching futures vs. values. I'm closing this for now until I come up with a better idea.

@craig-day craig-day closed this May 7, 2021
@craig-day
Copy link
Author

Alternative approach in #82

@craig-day craig-day deleted the craig-day/cache-values-not-futures branch May 7, 2021 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do I store the cache to a remote server CacheMap<K, V> is the wrong signature
1 participant