How to implement cache(redis cache) and Async communication in spring boot

Sanjay Singh
2 min readMar 28, 2023

--

Required scenario — when I am trying to get large amount data from db (postgresql) but we are getting API gateways time out issue , so we need to follow

Required scenario

How to implement Async communication in spring boot

step 1- 
use @EnableAsync in class level annotation over service class

@EnableAsync
public class ApprovalItemAsyncServiceImpl {

}
step 2-

use @Async in method level annotation inside service class

@Async
public List<ReportViewForYearModel> readDataFromCache(String startDate, String endDate, UserDetails userDetails) {


step 3

you need to call this method in noraml flow then async method will work

public class ApprovalItemServiceImpl{

@Autowired
private ApprovalItemAsyncServiceImpl approvalAsyncservice;

public void methodCall(){
approvalAsyncservice.readDataFromCache(startDate, endDate, userDetails);
}

}

How to implement redis cache in spring boot

we can store data on basis of key and value pair and using key we can get and delete the data.

step 1-
use @EnableCaching in class level annotation over service class

@EnableCaching
public class ApprovalItemAsyncServiceImpl {}


Step 2
use RedisTemplate to get method

@Autowired
private RedisTemplate<String, List<?>> redisTemplate;

step 3 use

@Async
public List<ReportViewForYearModel> readDataFromCache(String startDate, String endDate, UserDetails userDetails) {
log.info("Inside ApprovalItemAsyncServiceImpl() ::");
List<ReportViewForYearModel> viewDataList = null;

SimpleDateFormat formatter = new SimpleDateFormat(CesoaServiceConstants.REQUIRED_DATE_FORMATE_YMD);
Date beginDate;
try {
beginDate = formatter.parse(startDate);
Date last = formatter.parse(endDate);
log.info("start reading data from db in method readDataFromCache for user {}" + userDetails.getUserName());
List<ReportViewForYearModel> viewData = reportViewYearRepo.findByRequestedDateBetweenData(beginDate, last);
log.info("complite data reading from db user {}" + userDetails.getUserName());
// delete on basis of key
log.info("delete cache data for user {}" + userDetails.getUserName());
// delete data from cache (add data into cache)
// redisTemplate.delete(userDetails.getUserName());

// put data in cache (add data into cache)
log.info("put db data in cache for user {}" + userDetails.getUserName());
redisTemplate.opsForValue().set(userDetails.getUserName(), viewData);

// getData from cache
log.info("get data from cache for user {}" + userDetails.getUserName());
viewDataList = (List<ReportViewForYearModel>) redisTemplate.opsForValue().get(userDetails.getUserName());
log.info("complite data reding from cache for user {}" + userDetails.getUserName());

} catch (Exception e) {
log.info("get exception for user {}" + userDetails.getUserName(), e.getMessage());
e.getStackTrace();

}

return viewDataList;

}

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section