redis系列
序
本文主要展示怎麼在SpringDataRedis中使用pipeline。
pipeline概要
正常情況下,每個請求命令發出後client通常會阻塞並等待redis服務端處理,redis服務端處理完後將結果返回給client。當client使用pipeline傳送命令時,redis server必須部分請求放到佇列中(使用記憶體)執行完畢後一次性傳送結果。在一定程度上,可以較大的提升效能,效能提升的原因主要是TCP連結中較少了“互動往返”的時間。
使用
@Test
public void pipeline(){
List<Object> results = template.executePipelined(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
for(int i=0; i< 10; i++) {
stringRedisConn.lPush("myqueue","item"+i);
}
return null;
}
});
results.stream().forEach(System.out::println);
}
輸出
1
2
3
4
5
6
7
8
9
10
檢視redis
127.0.0.1:6379> lrange myqueue 0 -1
1) "item9"
2) "item8"
3) "item7"
4) "item6"
5) "item5"
6) "item4"
7) "item3"
8) "item2"
9) "item1"
10) "item0"
如果不需要返回結果,則可以使用redisTemplate的execute,然後傳入true給pipeline引數。
executePipelined原始碼
public List<Object> executePipelined(final RedisCallback<?> action, final RedisSerializer<?> resultSerializer) {
return execute(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
boolean pipelinedClosed = false;
try {
Object result = action.doInRedis(connection);
if (result != null) {
throw new InvalidDataAccessApiUsageException(
"Callback cannot return a non-null value as it gets overwritten by the pipeline");
}
List<Object> closePipeline = connection.closePipeline();
pipelinedClosed = true;
return deserializeMixedResults(closePipeline, resultSerializer, resultSerializer, resultSerializer);
} finally {
if (!pipelinedClosed) {
connection.closePipeline();
}
}
}
});
}
redis自帶的benchmark
redis自帶了redis-benchmark,可以用來測試redis的效能。不給定任何引數的情況下預設使用50個客戶端來進行效能測試。redis-benchmark不會處理執行命令所獲得的命令回覆,所以它節約了大量用於對命令回覆進行語法分析的時間。
redis-benchmark -c 1 -q
PING_INLINE: 58823.53 requests per second
PING_BULK: 60642.81 requests per second
SET: 59772.86 requests per second
GET: 60096.15 requests per second
INCR: 60532.69 requests per second
LPUSH: 60204.70 requests per second
LPOP: 58309.04 requests per second
SADD: 60350.03 requests per second
SPOP: 59066.75 requests per second
LPUSH (needed to benchmark LRANGE): 60313.63 requests per second
LRANGE_100 (first 100 elements): 37341.30 requests per second
LRANGE_300 (first 300 elements): 17934.00 requests per second
LRANGE_500 (first 450 elements): 13208.29 requests per second
LRANGE_600 (first 600 elements): 10604.45 requests per second
MSET (10 keys): 54171.18 requests per second