简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM