Search code examples
springspring-bootspring-mvccache-control

Exclude specific field from caching spring boot


I am trying to exclude a specific parameter (param 4) from caching in my rest spring boot application

I am new to the caching and not sure how can we exclude the specific parameter from caching

@Override
@Cacheable(value = "mykey")
public MyResponse myMethod(RestTemplate restTemplate, String param1, String param2, String param3, String param4){

I am expecting the param 4 field to be excluded from the caching


Solution

  • Key can be created using SpEL in @Cacheable annotation. Parameters can be accessed in SpEL using #aX X is index of argument. In this case.

    @Cacheable(value="mykey", key="#a1+#a2+#a3")

    https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#key--