Skip to content

@CachePut evaluates key before cache condition #22294

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

Closed
juanavelez opened this issue Jan 22, 2019 · 3 comments
Closed

@CachePut evaluates key before cache condition #22294

juanavelez opened this issue Jan 22, 2019 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another

Comments

@juanavelez
Copy link

juanavelez commented Jan 22, 2019

Affects: Spring Context 4.3.4 and higher


Trying to use @CachePut on a method that might return null.

@CachePut(cacheNames = Constants.Caches.USER, key = "#result.id", unless = "#result == null")
public MyObject doSomething();

You would expect the key expression not to be evaluated if the unless condition is true but this is not happening. The key is being evaluated before the unless condition is evaluated which leads to a

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'id' cannot be found on null

                if (cacheHit != null && cachePutRequests.isEmpty() && !hasCachePut(contexts)) {
			// If there are no put requests, just use the cache hit
			cacheValue = cacheHit.get();
			returnValue = wrapCacheValue(method, cacheValue);
		}
		else {
			// Invoke the method if we don't have a cache hit
			returnValue = invokeOperation(invoker);
			cacheValue = unwrapReturnValue(returnValue);
		}

		// Collect any explicit @CachePuts
		collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);

		// Process any collected put requests, either from @CachePut or a @Cacheable miss
		for (CachePutRequest cachePutRequest : cachePutRequests) {
			cachePutRequest.apply(cacheValue);
		}

The collectPutRequests tries to evaluate the key if the condition passes (there is no condition). I think the keys need to be evaluated only if unless is false?

@juanavelez juanavelez changed the title Spring Context CacheAspectSupport evaluates key BEFORE it executes unless for CachePut Spring Context CacheAspectSupport evaluates key BEFORE it executes unless for @CachePut Jan 22, 2019
@snicoll snicoll changed the title Spring Context CacheAspectSupport evaluates key BEFORE it executes unless for @CachePut @CachePut evaluates key before cache condition Jan 23, 2019
@sdeleuze sdeleuze added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 23, 2019
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 23, 2019
lgxbslgx added a commit to lgxbslgx/spring-framework that referenced this issue Apr 8, 2019
@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.
@solarmicrobe
Copy link

+1
Just hit this today. Luckily for me it is in a demo application and I can submit with just a javadoc on my function.

@sbrannen
Copy link
Member

sbrannen commented May 5, 2020

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@sbrannen sbrannen changed the title @CachePut evaluates key before cache condition @CachePut evaluates key before unless condition May 5, 2020
@sbrannen sbrannen changed the title @CachePut evaluates key before unless condition @CachePut evaluates key before cache condition May 5, 2020
@snicoll
Copy link
Member

snicoll commented Nov 16, 2021

Closing in favor of PR #22769

@snicoll snicoll closed this as completed Nov 16, 2021
@snicoll snicoll added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 16, 2021
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: superseded An issue that has been superseded by another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants