Galet
Galet

Reputation: 6279

How to cache with condition on result object in spring boot

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

Answers (1)

Ranjan
Ranjan

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

Related Questions