0
votes

I have used spring @Cacheable for caching as shown below:

@Cacheable
public Response mycall(Request request)

Now I want this method call to be cached only if request.getId()!=3, where getId() is a public getter method in Request class.

I have seen code wherein people have specified unless condition in @Cacheable, but I have seen conditions being specified only on Response of the method and not on Request. Reference: How do I tell Spring cache not to cache null value in @Cacheable annotation

Is there any way to achieve the same with condition being specified on a field of the request

1
Don't you want something like unless="#request.id == 3" ? It looks like parameters are available by name, and result as the name "#result", in the unless expression ... - moilejter
i tried it , not working - Manas Saxena

1 Answers

0
votes

Did you try the condition attribute?

//It will cache only if the actor's age is more than 20. Here actor is the input parameter.
@Cacheable(value="movies", key="#actor.name", condition="#actor.age > 20")
public List<movie> getMovies(Actor actor) ...

See ttps://www.codelooru.com/2017/03/spring-cache-part-3-conditional-cache.html