Gerardo
Gerardo

Reputation: 307

How to initialize redis with cached data

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?

Upvotes: 1

Views: 2042

Answers (2)

Shivansh Narayan
Shivansh Narayan

Reputation: 516

using JedisPool

public static void initCache() {
   jedisPool = new JedisPool( new JedisPoolConfig() , "localhost",8081);

 }

Upvotes: 1

Jayesh
Jayesh

Reputation: 999

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();
}

Upvotes: 1

Related Questions