I have a service built with spring boot and redis, which listens for HTTP requests.
I would like that when the server runs up, it populates some predefined data in redis cache.
I thought about having a .yml file and when the service starts it calls an endpoint like /addData with the information of that .yml file; but I think this is not an efficient way to achieve my goal.
Which is the best option to start a service with data cached in redis?
2 Answers 2
2 options either EventListener or on the main method
@EventListener(ApplicationReadyEvent.class)
public void loadRedis() {
 //do the work here
}
another option is to do it in springbootapplication main method.
public static void main(final String[] args) {
 ConfigurableApplicationContext context = 
 SpringApplication.run(Application.class, args);
 context.getBean(Whatever.class).loadRedis();
}
 Comments
using JedisPool
public static void initCache() {
 jedisPool = new JedisPool( new JedisPoolConfig() , "localhost",8081);
 }