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

Alejandro picture Alejandro · Apr 30, 2016 · Viewed 8.1k times · Source

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.

Answer

Tobsch picture Tobsch · Mar 7, 2017

Feign does not support caching. I would prefer the JCache (JSR-107) and maybe use it via the spring-boot-starter-cache described in the spring guide

JCache is an abstraction for the proprietary implementation of EhCache, Hazelcast, ... so it is possible to change the implementation with very less impact on the application. At first I would prefer EhCache 3.