How to cache REST API response in java
Asked Answered
S

2

12

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

Soever answered 8/4, 2017 at 14:7 Comment(1)
The caching system needs memory resource.Especially if api is invoking frequently.Actually your situation is not clear enough.Why do you call api 15000 times if the response is the same?Why did you decide that way.I think the solution is not right so describe your situation and we will try to give a solution.Handknit
W
13

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.

Walke answered 8/4, 2017 at 14:15 Comment(0)
H
12

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 :)

Hexyl answered 30/8, 2017 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.