redis系列html
redis的發佈訂閱功能java
redis消息隊列redis
redis的pipelinespring
redis的scan操做shell
本文主要展現怎麼在SpringDataRedis中使用pipeline。segmentfault
正常狀況下,每一個請求命令發出後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
查看rediscode
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參數。server
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自帶了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