Search code examples
springcachingnetflix-feignspring-cloud-netflixspring-cloud-feign

How to Cache HTTP Requests made with Netflix's Feign library in a Java Spring app


In a spring boot application using Netflix's Feign to make HTTP requests to a service, is there an easy way to cache and return these cached values automatically? The cache should be based on the parameters passed to the request (similar to memoizing a function for X minutes).

I'm looking for something like the @Cache annotation below:

@Component
@FeignClient(value = "facebook", url = "${auth.facebook.url}")
public interface FacebookClient {
    @Cache(600) // Something like this.
    @RequestMapping(method = RequestMethod.GET, value = "/debug_token?input_token={input_token}&access_token={access_token}")
    Map debugToken(@PathVariable("input_token") String inputToken, @PathVariable("access_token") String appToken);
}

Of course I could cache it myself using a decorator around the FacebookClient, I was wondering if there was a quicker/less code way.


Solution

  • Springs @Cacheable does what you need.

    Check: Caching Data with Spring