2020-10-29

Use cache by each HTTP Request

I have an AppleService with a cacheable method getApples

@Cacheable("getApples")
public List<Apple> getApples(){
  //fetches apples from external API
}

Then I have the following controller methods:

@GETMapping("/fruits")
getFruits(){
  fruitService.getFruits(); //that internally invokes appleService.getApples();
}


@POSTMapping()
postRequest(){
  aService; //that internally invokes appleService.getApples();
  anotherService; //that internally invokes appleService.getApples();

}

This Spring application is consumed by a frontend that first invokes the @GETMapping("/fruits") method to load info in the page. Later, when the user submits the post request it should first (in aService) make another call to the external API invoked in appleService.getApples() without using cache and the cache should only be used in the anotherService when this invokes appleService.getApples().

However what is happening is that after the getFruits request is invoked to load the page info, then it caches getApples method. And when the user submits the @POSTMapping() request it is using cache in both appleService.getApples() method calls in aService and anotherService.

So how can I use cache only by each http request and not keep cache between different http requests?



from Recent Questions - Stack Overflow https://ift.tt/3muI4fB
https://ift.tt/eA8V8J

No comments:

Post a Comment