Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load data into redis cache from database?

We are planning to use Redis cache in our app , our requirement is first we need to load some 6 month data into Redis cache before actual app starts , i am thinking that if we execute one command at a time in loop like to insert key values into Redis , it will take too much time , is their way we can retrieve the data from database and insert all the data into Redis in one shot ?

can any one please suggest ?

like image 612
Bravo Avatar asked Mar 12 '26 11:03

Bravo


1 Answers

Redis provides support for pipelining, which involves sending multiple commands to the server without waiting for the replies and then reading the replies in a single step. Pipelining can improve performance when you need to send several commands in a row, such as adding many elements to the same List.

Spring Data Redis provides several RedisTemplate methods for executing commands in a pipeline.

One example:

//pop a specified number of items from a queue
List<Object> results = stringRedisTemplate.executePipelined(
  new RedisCallback<Object>() {
    public Object doInRedis(RedisConnection connection) throws DataAccessException {
      StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
      for(int i=0; i< batchSize; i++) {
        stringRedisConn.rPop("myqueue");
      }
    return null;
  }
});

You can follow this link

Or you can use Redis Mass insertion facility too.

like image 90
Yogi Avatar answered Mar 15 '26 03:03

Yogi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!