Skip to content

Instantly share code, notes, and snippets.

@agrison
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrison/d4ea24b3687084bc85f9 to your computer and use it in GitHub Desktop.
Save agrison/d4ea24b3687084bc85f9 to your computer and use it in GitHub Desktop.
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