Reputation: 6279
I want to cache the result in spring boot with condition on the result.
@Cacheable(value = "saveCache", key = "{#a, #b, #c}")
public Response save(String a, String b, String c) {
// block of code
List<Map<String, Object>> result = new ArrayList();
new Response(result)
}
In the above code, I want to cache only if response.result is not empty. I have tried the below method, but it doesn't work for me
@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result == null")
@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.size > 0")
[Error] EL1008E: Property or field 'size' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.length > 0")
[Error] EL1008E: Property or field 'length' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
Upvotes: 3
Views: 4232
Reputation: 400
As size is a method of ArrayList. Please use it like this.
@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.size() > 0")
For ref: SpringBoot Cacheable unless result with List
Upvotes: 2