6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jedis でパイプライニングするサンプル

Posted at

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倍早くなるとのこと。

6
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?