I am trying to create a cache key with multiple parameter values.
@Cacheable(cacheNames = "cache_name_", key = "#name + '_' + #id")
private JSONObject getData(String name, String id) throws Exception {
From the above scenario, I name is a mandatory field while id is optional. I want to create key in such a way that,
Currently, the key is forming something like "cache_name_test_null" if the id is not passed in the parameter value
Is it possible to create such key with @Cacheable annotation?
It's doable, but you need to wrap 2 @Cachable annotations in a @Caching annotation.
@Caching(
cacheable = {
@Cacheable(cacheNames = "cache_name_", key = "#name + '_' + #id", condition = "#id != null"),
@Cacheable(cacheNames = "cache_name_", key = "#name", condition = "#id == null")
}
)
public JSONObject getData(String name, String id) throws Exception {
You are using the @Caching annotation on a private method. This doesn't work. Those annotations only work on public methods being called from outside the class. See this Stack Overflow answer: https://stackoverflow.com/a/16899739/2082699