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

Support for custom cache registration in CaffeineCacheManager #25230

Closed
T3rm1 opened this issue Jun 10, 2020 · 5 comments
Closed

Support for custom cache registration in CaffeineCacheManager #25230

T3rm1 opened this issue Jun 10, 2020 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@T3rm1
Copy link

T3rm1 commented Jun 10, 2020

Affects: 5.2.6


It is quite common to have multiple caches with different value lifetime. Right now CaffeineCacheManager creates every Cache with the default settings or the settings set through either of setCacheLoader, setCacheSpecification or setCaffeineSpec. But those settings apply to all Caches.
If cacheMap would be exposed (maybe just a simple getter) it would be possible to add prebuilt caches with custom settings to it.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 10, 2020
@jhoeller
Copy link
Contributor

jhoeller commented Jun 10, 2020

I suppose we could alternatively introduce dedicated registration methods for individual caches, e.g. taking cache name plus cache specification, or potentially taking a callback for creating the native cache instance... I'd rather make the use case first-class than expose the inner map here, e.g.: registerCache(String name, com.github.benmanes.caffeine.cache.Cache cache)

If you'd like full control of individual caches, you could also use SimpleCacheManager and populate it with a collection of CaffeineCache instances of your choice.

@T3rm1
Copy link
Author

T3rm1 commented Jun 10, 2020

I like the idea with registerCache(String name, com.github.benmanes.caffeine.cache.Cache cache) which then puts the cache into the map. I think it is better than the callback approach.

@jhoeller
Copy link
Contributor

I've just implemented such a registerCustomCache method, the only hurdle having been that setCaffeineSpec etc calls on the CaffeineCacheManager should only reinitialize default caches but not such custom cache registrations (that's also the reason for the registerCustomCache naming). In any case, it seems to flow nicely with any kind of Caffeine.newBuilder() or Caffeine.from(...) usage.

Alright, I'll roll this into 5.2.8 right away if this matches your use case as well :-)

@jhoeller jhoeller changed the title Expose cacheMap in CaffeineCacheManager Support for custom cache registration in CaffeineCacheManager Jun 10, 2020
@jhoeller jhoeller self-assigned this Jun 10, 2020
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 10, 2020
@jhoeller jhoeller added this to the 5.2.8 milestone Jun 10, 2020
@T3rm1
Copy link
Author

T3rm1 commented Jun 10, 2020

Awesome! Thanks for doing this so fast. 🍺

@Praveen1027
Copy link

Hi. Is there a way I can get the generics in place while attempting to registerCustomCache(spring), something like :
Cache<String, String> cityCache = Caffeine.newBuilder() .expireAfterWrite(1, TimeUnit.DAYS).maximumSize(1000).build(); caffeineCacheManager.registerCustomCache("city_data", cityCache);// this fails

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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants