redis的pipeline

redis系列

  • redis的发布订阅功能

  • redis消息队列

  • redis的pipeline

  • redis的scan操作

本文主要展示怎么在SpringDataRedis中使用pipeline。

pipeline概要

正常情况下,每个请求命令发出后client通常会阻塞并等待redis服务端处理,redis服务端处理完后将结果返回给client。当client使用pipeline发送命令时,redis server必须部分请求放到队列中(使用内存)执行完毕后一次性发送结果。在一定程度上,可以较大的提升性能,性能提升的原因主要是TCP链接中较少了“交互往返”的时间。

使用

@Test
    public void pipeline(){
        List results = template.executePipelined(new RedisCallback() {
                    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 executePipelined(final RedisCallback action, final RedisSerializer resultSerializer) {
        return execute(new RedisCallback>() {
            public List 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 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

参考

  • spring-data-redis-pipeline

  • Redis的新特性:pipeline(管道)

你可能感兴趣的:(redis)