JavaのRedisクライアントであるJedis (https://github.com/xetorthio/jedis) で、パイプライニングを使用する場合のサンプル。
パイプライニングしない場合
Jedis jedis = null;
try {
jedis = new Jedis("localhost");
jedis.set("foo", "bar");
} finally {
if (jedis != null) {
jedis.disconnect();
}
}
パイプライニングする場合(その1)
Jedis jedis = null;
try {
jedis = new Jedis("localhost");
Pipeline pipeline = jedis.pipelined();
pipeline.set("foo", "bar");
pipeline.sync();
} finally {
if (jedis != null) {
jedis.disconnect();
}
}
パイプライニングする場合(その2)
Jedis jedis = null;
try {
jedis = new Jedis("localhost");
jedis.pipelined(new PipelineBlock() {
@Override
public void execute() {
set("foo", "bar");
sync();
}
});
} finally {
if (jedis != null) {
jedis.disconnect();
}
}
サンプルの様な単発のコマンド発行だと、パイプライニングの恩恵はほとんどありませんが、バッチ処理などで一気に大量にRedisに書き込む場合などは、高速な処理が期待できます。
http://redis.shibu.jp/developer/pipelining.html によると、4倍早くなるとのこと。