spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.75k stars 38.15k forks source link

Deregister empty `Cache` from `CacheManager` #33813

Closed trung closed 3 weeks ago

trung commented 3 weeks ago

I use CaffeineCacheManager and create temporary caches dynamically for each request. When a request completes, the cache items are evicted, leaving the cache instances empty but still in memory.

CaffeineCacheManager has an internal cacheMap which keeps growing as caches are dynamically created.

Is there away to remove/deregister the empty cache instances from cacheMap?

jhoeller commented 3 weeks ago

Good point, I'll add a removeCache(String name) method on CaffeineCacheManager for 6.1.15.

trung commented 3 weeks ago

Please also add removeCache(String name) for ConcurrentMapCacheManager as well. I'm not using it but I do see it has similar need.

ben-manes commented 3 weeks ago

@jhoeller is instead @Bean @RequestScoped CacheManager the intended idiom? (that was my best guess when asked in https://github.com/ben-manes/caffeine/discussions/1790)

jhoeller commented 3 weeks ago

@ben-manes that would indeed work but I can also see the need for runtime removal of certain caches in a shared CacheManager scenario. As long as the cache names are distinct enough, I can see that working for short-lived request caches as well.