Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple spring boot app that inserts 1M random key into redis using one connection and pipelining
package me.grison.redis.foo;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.*;
import redis.clients.jedis.*;
import java.util.UUID;
@ComponentScan
@Configuration
@SpringBootApplication
@EnableAutoConfiguration
public class RedisMassImport {
@Bean
JedisPool jedisPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setTestOnBorrow(true);
poolConfig.setTestWhileIdle(true);
return new JedisPool(poolConfig, "localhost", 6379, 30);
}
@Bean
CommandLineRunner init(JedisPool pool) {
return args -> {
int numKeys = 1_000_000;
try (Jedis jedis = pool.getResource()) {
// prepare UUIDs
String[] uuids = new String[numKeys];
for (int i = 0; i < numKeys; ++i)
uuids[i] = UUID.randomUUID().toString();
long time = System.nanoTime();
Pipeline p = jedis.pipelined();
for (int i = 0; i < numKeys; ++i)
p.set(uuids[i], uuids[i]);
p.sync();
long endTime = System.nanoTime();
System.out.println("\tTook " + (endTime - time) + "ns to sync the pipeline.");
}
};
}
public static void main(String[] args) {
SpringApplication.run(RedisMassImport.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment