Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

443
Views
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

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!