2

I have a weird case where my @CacheEvict is not working. Code looks like this:

@Caching(evict = {
        @CacheEvict(value = CacheConsts.C_CACHE1, keyGenerator = CacheConsts.KG_CACHE1, condition="#someModel != null && #someModel.getSomeProperty() != null"),
        @CacheEvict(value = CacheConsts.C_CACHE2, keyGenerator = CacheConsts.KG_CACHE2, condition="#someModel != null && #someModel.getSomeProperty() != null"),
        @CacheEvict(value = CacheConsts.C_CACHE3, keyGenerator = CacheConsts.KG_CACHE3, condition="#someModel != null && #someModel.getSomeProperty() != null")
})
public boolean addModel(ModelDTO someModel, String tenant);

However, it works when I remove the condition!!! Even though all data I test with is non-null.

Ex: when I remove this: "#someModel != null && #someModel.getSomeProperty() != null", it works.

I'm testing with a ModelDTO that is not null and the "someProperty" is not null either.

It seems to me that the condition would pass and it would evict.. but that is not what's happening.

Any ideas?

Is my Spel properly formed?

Why would my cache not evict here?

Does this have something to do with the @Caching annotation or some behavior of CacheEvict's condition that I'm unaware of?

Thanks for any help or ideas folks.

1 Answer 1

3

AFAICT, your SpEL expression and condition seem correct.

You might want to verify your compiler has debug set to true, which is required to reference method parameters by "name" since the compiler will then include variable names in the Java bytecode.

You might also try referring to method parameters in your SpEL expression, generically (for debugging purposes), such as: #a0 != null && #a0.someProperty(); refer to this section in the docs.

Finally, I have written a simple Integration Test simulating your UC above.

The test and supporting code (all contained in the referenced test class) are similar to an extent. However, my code is slightly different in that I am not using a custom KeyGenerator (per Cache), therefore I did not require the containing Caching annotation (I simply used cacheNames), but that should be of little consequence. The test passed!

Hopefully, this will give you more ideas.

Feel free to play around with my test to experiment as necessary.

Cheers!

5
  • Thanks John! I'll take a look. Regarding the comment about debug in the compiler. Am I understanding correctly that SpEL may not be interpreted correctly depending on compiler settings? So perhaps the condition is not working depending on compiler settings if I reference method params by name instead of position?
    – TheJeff
    Jan 29, 2021 at 18:20
  • That's correct. I found this short article that might explain it better (logicbig.com/how-to/java-command/compile-with-debug-info.html). Typically, IDE's & build tools (e.g. Maven Compiler Plugin: maven.apache.org/plugins/maven-compiler-plugin/…) enable this compiler option by default. So, I am not entirely certain this is your problem, but is something to check.
    – John Blum
    Jan 29, 2021 at 18:50
  • 1
    By way of example, if I modify the condition attribute in the @Cacheable annotation on the UserService.process(:User) method in my test class to condition = "#user != null && #user.name != null && #nonExistingArgument != null", then the test fails here (github.com/jxblum/stackoverflow-questions-answers/blob/master/…). That is, the User ("jonDoe") is never added to the "Admin", "Users" and "Guests" caches.
    – John Blum
    Jan 29, 2021 at 18:56
  • 1
    This means that if there is no (SpEL) context information for a referenced variable (e.g. nonExistingArgument), then SpEL cannot resolve any information about the var and the condition will fail.
    – John Blum
    Jan 29, 2021 at 18:58
  • Well this is very helpful and you know how fast projects can move so I won't be going back to verify.. just going to mark it so I don't leave it hanging. I'm pretty convinced this must be it. Would be great if the tutorials mentioned this way rather than the named vars way. Either way its a great answer, thanks John. +++
    – TheJeff
    Feb 1, 2021 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.