-
Notifications
You must be signed in to change notification settings - Fork 38.1k
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
@CachePut evaluates key before cache condition #22769
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Cacheable, @CacheEvict and @CachePut evaluate their keys after their cache condition which contains the `condition` and `unless`. Add the corresponding test cases. Fixes spring-projects#22294.
@Cacheable, @CacheEvict and @CachePut evaluate their conditions and keys after they get the result of the method, so the `#result` can be used.
spring-projects-issues
added
the
status: waiting-for-triage
An issue we've not yet triaged or decided on
label
Apr 8, 2019
rstoyanchev
added
the
in: core
Issues in core modules (aop, beans, core, context, expression)
label
Nov 10, 2021
snicoll
added
type: enhancement
A general enhancement
and removed
status: waiting-for-triage
An issue we've not yet triaged or decided on
labels
Nov 16, 2021
When will this fix be merged in to master ? We have this problem in our code. |
# Conflicts: # spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java # spring-context/src/test/java/org/springframework/cache/config/AbstractCacheAnnotationTests.java # spring-context/src/testFixtures/java/org/springframework/context/testfixture/cache/beans/AnnotatedClassCacheableService.java
After merging main branch, some previous test cases missed.
I merged the main branch and solved the conflicts just now. |
It took me a bit to understand why the evaluation of cacheable was moved as it felt unrelated but evaluating the unless expression earlier means that we need the result for cacheable as well. |
snicoll
pushed a commit
that referenced
this pull request
Jul 14, 2023
Prior to this commit a @CachePut operation would fail if the key expression is invalid, but guarded with an unless condition as the former was evaluated too early. This commit makes sure that key for a put is only evaluated if the put operation is active. Note that this does not apply for @Cacheable as the key needs to be computed early to determine if a matching entry exists in the cache. See gh-22769
snicoll
added a commit
that referenced
this pull request
Jul 14, 2023
snicoll
changed the title
Evaluating key after cache condition
@CachePut evaluates key before cache condition
Jul 14, 2023
snicoll
added
type: bug
A general bug
and removed
type: enhancement
A general enhancement
labels
Jul 14, 2023
@lgxbslgx thank you for making your first contribution to Spring Framework. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At present,
@Cacheable
,@CacheEvict
and@CachePut
evaluate their keys aftercondition
but beforeunless
. I think the keys should be evaluated after all the cache condition which contains thecondition
andunless
.I change the code, add the corresponding test cases and fix the JavaDoc for @Cacheable, @CacheEvict and @CachePut.
Fixes #22294.