Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@NYgomets
Copy link
Contributor

Description

This PR fixes a memory leak in CaffeineCacheManager where dynamically created caches were not removed when switching to static mode via setCacheNames().

Changes

Core Fix

  • Modified CaffeineCacheManager.setCacheNames() to clear dynamically created caches while preserving custom caches
  • Updated Javadoc to clarify the behavior

Tests Added

  • setCacheNamesShouldRemovePreviousDynamicCaches(): Verifies dynamic caches are cleared
  • setCacheNamesShouldPreserveCustomCaches(): Ensures custom caches are not affected
  • switchingBetweenDynamicAndStaticMode(): Tests mode transitions

Backward Compatibility

This is a potentially breaking change for users who rely on the (undocumented) behavior of dynamic caches persisting after setCacheNames().

However:

  1. The current behavior violates the documented contract
  2. The memory leak justifies the fix
  3. The typical use case (setting static caches at startup) is unaffected

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 14, 2025
@NYgomets NYgomets closed this Nov 14, 2025
@NYgomets NYgomets reopened this Nov 14, 2025
@NYgomets NYgomets force-pushed the fix-cache-manager-static-mode-leak branch 5 times, most recently from b86be0c to 8e43a3f Compare November 14, 2025 17:37
When setCacheNames() is called to switch from dynamic to static mode,
dynamically created caches were not removed from the internal cacheMap,
causing:
1. Memory leak (orphaned cache references)
2. Violation of Javadoc contract stating cache names will be 'fixed'
3. getCacheNames() returning unexpected cache names

This fix ensures that dynamically created caches are cleared when
switching to static mode, while preserving custom caches registered
via registerCustomCache().

Signed-off-by: ParkJuhyeong <[email protected]>
@NYgomets NYgomets force-pushed the fix-cache-manager-static-mode-leak branch from 8e43a3f to 47706be Compare November 14, 2025 17:52
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 15, 2025
@sbrannen sbrannen self-assigned this Nov 15, 2025
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: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants