一般情況下,Redis Client端發(fā)出一個(gè)請(qǐng)求后,通常會(huì)阻塞并等待Redis服務(wù)端處理,Redis服務(wù)端處理完后請(qǐng)求命令后會(huì)將結(jié)果通過(guò)響應(yīng)報(bào)文返回給Client。
感覺(jué)這有點(diǎn)類似于HBase的Scan,通常是Client端獲取每一條記錄都是一次RPC調(diào)用服務(wù)端。
在Redis中,有沒(méi)有類似HBase Scanner Caching的東西呢,一次請(qǐng)求,返回多條記錄呢?
有,這就是Pipline。官方介紹 http://redis.io/topics/pipelining
通過(guò)pipeline方式當(dāng)有大批量的操作時(shí)候,我們可以節(jié)省很多原來(lái)浪費(fèi)在網(wǎng)絡(luò)延遲的時(shí)間,需要注意到是用pipeline方式打包命令發(fā) 送,redis必須在處理完所有命令前先緩存起所有命令的處理結(jié)果。打包的命令越多,緩存消耗內(nèi)存也越多。所以并不是打包的命令越多越好。
使用Pipeline在對(duì)Redis批量讀寫(xiě)的時(shí)候,性能上有非常大的提升。
Java測(cè)試了一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package com.lxw1234.redis; import java.util.HashMap; import java.util.Map; import java.util.Set; import redis.clients.jedis.Jedis; import redis.clients.jedis.Pipeline; import redis.clients.jedis.Response; public class Test { public static void main(String[] args) throws Exception { Jedis redis = new Jedis( "127.0.0.1" , 6379 , 400000 ); Map<String,String> data = new HashMap<String,String>(); redis.select( 8 ); redis.flushDB(); //hmset long start = System.currentTimeMillis(); //直接hmset for ( int i= 0 ;i< 10000 ;i++) { data.clear(); data.put( "k_" + i, "v_" + i); redis.hmset( "key_" + i, data); } long end = System.currentTimeMillis(); System.out.println( "dbsize:[" + redis.dbSize() + "] .. " ); System.out.println( "hmset without pipeline used [" + (end - start) / 1000 + "] seconds .." ); redis.select( 8 ); redis.flushDB(); //使用pipeline hmset Pipeline p = redis.pipelined(); start = System.currentTimeMillis(); for ( int i= 0 ;i< 10000 ;i++) { data.clear(); data.put( "k_" + i, "v_" + i); p.hmset( "key_" + i, data); } p.sync(); end = System.currentTimeMillis(); System.out.println( "dbsize:[" + redis.dbSize() + "] .. " ); System.out.println( "hmset with pipeline used [" + (end - start) / 1000 + "] seconds .." ); //hmget Set<String> keys = redis.keys( "*" ); //直接使用Jedis hgetall start = System.currentTimeMillis(); Map<String,Map<String,String>> result = new HashMap<String,Map<String,String>>(); for (String key : keys) { result.put(key, redis.hgetAll(key)); } end = System.currentTimeMillis(); System.out.println( "result size:[" + result.size() + "] .." ); System.out.println( "hgetAll without pipeline used [" + (end - start) / 1000 + "] seconds .." ); //使用pipeline hgetall Map<String,Response<Map<String,String>>> responses = new HashMap<String,Response<Map<String,String>>>(keys.size()); result.clear(); start = System.currentTimeMillis(); for (String key : keys) { responses.put(key, p.hgetAll(key)); } p.sync(); for (String k : responses.keySet()) { result.put(k, responses.get(k).get()); } end = System.currentTimeMillis(); System.out.println( "result size:[" + result.size() + "] .." ); System.out.println( "hgetAll with pipeline used [" + (end - start) / 1000 + "] seconds .." ); redis.disconnect(); } } |
測(cè)試結(jié)果如下:
1
2
3
4
5
6
7
8
|
dbsize:[10000] .. hmset without pipeline used [243] seconds .. dbsize:[10000] .. hmset with pipeline used [0] seconds .. result size:[10000] .. hgetAll without pipeline used [243] seconds .. result size:[10000] .. hgetAll with pipeline used [0] seconds .. |
使用pipeline來(lái)批量讀寫(xiě)10000條記錄,就是小菜一碟,秒完。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://my.oschina.net/u/2273085/blog/419920