-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Feature request: non-blocking invalidate() in AsyncLoadingCache #148
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
Comments
It is blocking only to perform the hash table operation. The value isn't returned, so it won't block on the future result. I don't think that deserves an asynchronous wrapping, do you? |
According to my experiments it blocks if a concurrent load is happening for a given key, see my test: https://github.com/softwaremill/cache-get-or-create/blob/master/src/main/java/com/softwaremill/TestCaffeine.java (the loader artificially sleeps for one second) |
Yes, but an async cache performs a load that returns a future which is very cheap. Your test is for a sync cache where the load can be slow due to performing the user operation. |
Ah! True :) And even though I'll put a blog on that up tomorrow probably :) - thanks for the project |
Yep, it's removed and the removal notification is chained on the future. The asMap().remove(k) does block on the future since it returns the result, so invalidating demonstrates an optimization by the more opinionated API. |
Managed to finish it today, here's the blog I mentioned: https://softwaremill.com/race-condition-cache-guava-caffeine/ |
Cool, thanks! You might want to use getIfPresent in your code examples, as get() was confusing if familiar with the interfaces. There is a Guava bug on the invalidation problem. Since the entry isn't loaded Guava let's removal skip, which simplifies the locking. Caffeine has the same problem on a clear since ConcurrentHashMap suppresses the keys being loaded when we iterate. |
Thanks for the pointer, adding a note to the blog. I think |
In your first example |
Ah there, correct, thanks, I was thinking of the later examples :) |
You can use invalidate(key) from the synchronous view. This is equivalent to the async asMap() remove(key) except doesn’t return the prior future mapping. The synchronous asMap().remove(key) does block on the future as that view has to return the value and wait until it’s resolved. |
Thanks, Ben! |
Right now the only way to invalidate a key in
AsyncLoadingCache
is through the synchronous interface, which provides a blocking method. It would be great if there was an asynchronous version, which would return aCompletableFuture<Void>
, which would complete when the key is invalidated.The text was updated successfully, but these errors were encountered: