首页 > 数据库 > Redis > 正文

SpringBoot中如何通过整合Redis实现管道

WBOY
发布: 2023-05-30 09:19:56
转载
1549人浏览过

1. Redis 之管道(pipeline)

执行一个Redis命令,Redis客户端和Redis服务器就需要执行以下步骤:

  • 客户端发送命令到服务器;

  • 服务器接受命令请求,执行命令,产生相应的结果;

  • 服务器返回结果给客户端;

  • 客户端接受命令的执行结果,并向用户展示。

Redis命令所消耗的大部分时间都用在了发送命令请求和接收命令结果上面,把任意多条Redis命令请求打包在一起,然后一次性地将它们全部发送给服务器,而服务器则会把所有命令请求都处理完毕之后,一次性地将它们的执行结果全部返回给客户端。

注意事项:

Redis服务器并不会限制客户端在管道中包含的命令数量,但是却会为客户端的输入缓冲区设置默认值为1GB的体积上限:当客户端发送的数据量超过这一限制时,Redis服务器将强制关闭该客户端。因此最好不要一下把大量命令或者一些体积非常庞大的命令放到同一个管道中执行。

除此之外,很多客户端本身也带有隐含的缓冲区大小限制,如果你在使用流水线特性的过程中,发现某些流水线命令没有被执行,或者流水线返回的结果不完整,那么很可能就是你的程序触碰到了客户端内置的缓冲区大小限制。

2. SpringBoot 整合 Redis 管道实例

SpringBoot 整合 redis 的实例

使用单个的 increment 命令,处理 200w个key:

public class RedisPipelineStudy extends BaseTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String PREFIX = "test0:";

    @Test
    public void test() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("test0");
        for (int times = 0; times < 2; times++) {
            for (int i = 0; i < 1000000; i++) {
                stringRedisTemplate.opsForValue().increment(PREFIX + i, 1L);
            }
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

}
登录后复制

耗时如下所示:是 12 位 ,单位ns

SpringBoot中如何通过整合Redis实现管道

使用管道 incrBy 处理 200w个key,每次打包300条命令发送给服务器,如下所示:

public class RedisPipelineStudy extends BaseTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String PREFIX = "test1:";

    @Test
    public void test() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("test1");
        List<Integer> recordList = new ArrayList<>();
        for (int times = 0; times < 2; times++) {
            for (int i = 0; i < 1000000; i++) {
                try {
                    recordList.add(i);
                    if (recordList.size() > 300) {
                        incrByPipeline(recordList);
                        recordList = new ArrayList<>();
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
            if (!CollectionUtils.isEmpty(recordList)) {
                incrByPipeline(recordList);
                recordList = new ArrayList<>();
            }
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

    private void incrByPipeline(List<Integer> recordList) {
        stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                try {
                    for (Integer record : recordList) {
                        byte[] key = (PREFIX + record).getBytes();
                        connection.incrBy(key, 1);
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
                return null;
            }
        });
    }
}
登录后复制

耗用时间: 11 位 ,单位 :ns,是单个命令耗时的 1/6。

SpringBoot中如何通过整合Redis实现管道

以上就是SpringBoot中如何通过整合Redis实现管道的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号