Skip to content
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

CaffeineCacheManager supports async loading value #30122

Closed
wants to merge 1 commit into from
Closed

CaffeineCacheManager supports async loading value #30122

wants to merge 1 commit into from

Conversation

jizhuozhi
Copy link

No description provided.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 15, 2023
@bclozel
Copy link
Member

bclozel commented Mar 15, 2023

Sorry, but we don't accept PRs without any rationale nor explanation, especially for new features and new API.

If I understand correctly, this change would turn cache entries into CompletableFuture<V>. As outlined in #17559, this is not supported (and it won't be supported by the JRS, see jsr107/jsr107spec#312). If you wish to use this feature, you can use Caffeine directly. We might reconsider this in the future with #17920, but we're missing critical bits right now to work on that.

Also, please run the build locally before submitting a PR - this change is failing the build.
Thanks!

@bclozel bclozel closed this Mar 15, 2023
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 15, 2023
@jizhuozhi
Copy link
Author

jizhuozhi commented Mar 15, 2023

Hello, @bclozel . It's my mistake. I just ran the test but forgot to check the style.

My intention is not to get CompletableFuture to make asynchronous calls, but to be able to use asynchronous API when refreshing the cache (this can avoid executor blocking).

The problem I have encountered is that when using Spring Cloud LoadBalancer (it uses CaffeineCacheManager to cache service instances), I don't want to get the instance from the registry when it expires, but pull the latest content from the registry regularly And replace the content in the cache (Caffeine provides the ability to refresh regularly). Most registry SDKs provide asynchronous APIs to avoid blocking the calling thread, but now CaffeineCacheManager only supports synchronous loading of values, so I have to convert asynchronous to synchronous and block here, which will affect the update of other cache values.

In fact, because com.github.benmanes.caffeine.cache.AsyncLoadingCache#synchronous is used to create a view of cache, for the caller of the cache, it is still synchronously obtaining data, and only the IO that occurs during refresh is affected operation only

@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: declined A suggestion or change that we don't feel we should currently apply labels Jul 21, 2023
@jhoeller jhoeller self-assigned this Jul 21, 2023
@jhoeller jhoeller modified the milestone: 6.1.0-M4 Jul 21, 2023
@jhoeller jhoeller added the status: declined A suggestion or change that we don't feel we should currently apply label Jul 21, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Jul 21, 2023

While this has been declined initially (and cannot be reopened anymore as a pull request), I'm effectively addressing this along with #17920 now. CaffeineCacheManager has a general setAsyncCacheMode(boolean) method now, as well as a setAsyncCacheLoader method which implicitly sets that flag as well. We not only build internally async caches, we also leverage the Caffeine AsyncCache interface for our new CompletableFuture-based retrieve operations now.

jhoeller added a commit that referenced this pull request Jul 21, 2023
…urn values

Includes CompletableFuture-based retrieve operations on Spring's Cache interface.
Includes support for retrieve operations on CaffeineCache and ConcurrentMapCache.
Includes async cache mode option on CaffeineCacheManager.

Closes gh-17559
Closes gh-17920
Closes gh-30122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants