Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache REST API response in java

I am building an app in java.I hit api more than 15000 times in loop and get the response ( response is static only )

Example

**
  username in for loop   
  GET api.someapi/username
  processing
  end loop
**

It is taking hours to complete all the calls. Suggest me any way (any cache technology) to reduce the call time.

P.S :

1) i am hitting api from java rest client(Spring resttemplate)

2) that api i am hitting is the public one, not developed by me

3) gonna deploy in heroku

like image 695
thaveethu gce Avatar asked Apr 08 '17 14:04

thaveethu gce


2 Answers

Try using Springs Cache Abstraction, docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html.

You can use this abstraction in the method which has the restTemplate call.

Any method calls response can be cached using this abstraction, with the method parameters as the keys and the return type as the response.

@Cacheable("username")
public UserResponse getUser(String username){
   // Code to call your rest api
}

This creates a Spring AOP advice around the method. Every time the method is called it checks if the data is available in the cache for this key(username), if yes then returns the response from the Cache and not calls the actual method. If the data is not available in the Cache then it calls the actual method and caches the data in the cache, so next time when the same method is called with same key the data can be picked from Cache.

This cache abstraction can be backed by simple JVM caches like Guava or more sophisticated cache implementations like EHCache, Redis, HazelCast as well.

like image 180
Praneeth Ramesh Avatar answered Oct 15 '22 05:10

Praneeth Ramesh


One very important note to that answer: If you ever plan to update those (cached) values, don't forget to use @CacheEvict on save() and delete() in the repositories. Else you will have problems fetching the new record when it is updated.

I have implemented my solution (with EhCache) this way (in the repository):

CurrencyRepository.java: // define a cacheable statement

@Cacheable("currencyByIdentifier")
public Currency findOneByIdentifier(String identifier);

CacheConfiguration.java: // Define that cache in EhCache Configuration

@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
    return cm -> {
        cm.createCache("currencyByIdentifier", jcacheConfiguration);
        cm.createCache("sourceSystemByIdentifier", jcacheConfiguration);
    };
}

CurrencyRepository.java: // evict on save and delete by overriding the default method

@Override
@CacheEvict("currencyByIdentifier")
<S extends Currency> S save(S currency);

@Override
@CacheEvict("currencyByIdentifier")
void delete(Currency currency);

I hope that helps :)

like image 12
dave0688 Avatar answered Oct 15 '22 06:10

dave0688