5

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.

Alejandro
  • 125
  • 1
  • 7

3 Answers3

3

Springs @Cacheable does what you need.

Check: Caching Data with Spring

Gergely Toth
  • 6,638
  • 2
  • 38
  • 40
  • 1
    Thanks for tip. For other who may read this, I found these helpful: [1](http://stackoverflow.com/questions/8181768/can-i-set-a-ttl-for-cacheable) [2](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-cache) – Alejandro May 06 '16 at 12:48
3

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.

Tobsch
  • 175
  • 2
  • 10
1

Feign client doesn't support caching.

Another better way would be to create a Service class which calls FeignClient and put up cache on methods of this new Service class.

Dhruv Saksena
  • 209
  • 2
  • 13