栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 的 RedisTemplate 的 execute 和 executePipelined 功能的区别redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 的 RedisTemplate 的 execute 和 executePipelined 功能的区别redis

1.executespring

如下是 springboot 官网原文:springboot

Redis provides support for transactions through the multi, exec, and discard commands. These operations are 
available on RedisTemplate, however RedisTemplate is not guaranteed to execute all operations in the 
transaction using the same connection.ide

Spring Data Redis provides the SessionCallback interface for use when multiple operations need to be performed 
with the same connection, as when using Redis transactions. For example:

//execute a transaction
List txResults = redisTemplate.execute(new SessionCallback() {
public List execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForSet().add(“key”, “value1”); // This will contain the results of all ops in the transaction return operations.exec(); } });

翻译下来就是:code

Redis 经过multi, exec, discard 操做提供事务支持. RedisTemplate 也一样支持这些操做, 然而 RedisTemplate 不保证在同一个链接中执行事务中的全部操做.orm

当使用 redis 的事务的时候, Spring Data Redis 提供 SessionCallback 的接口支持多个操做的执行都在同一个链接中.

2.Pipeline
Redis provides support for pipelining, which involves sending multiple commands to the server without waiting
 for the replies and then reading the replies in a single step. Pipelining can improve performance when you 
 need to send several commands in a row, such as adding many elements to the same List.

Spring Data Redis provides several RedisTemplate methods for executing commands in a pipeline. If you don't 
care about the results of the pipelined operations, you can use the standard execute method, passing true for 
the pipeline argument. The executePipelined methods will execute the provided RedisCallback or SessionCallback 
in a pipeline and return the results. For example:

Redis 提供 pipelining(管道) 的支持, 它能够发送多条指令到 redis服务端 而不用等待服务端的回复 而且 读取服务端的回复在一步操做中. 当你须要连续发送多条命令的时候 Pipelining(管道) 能够改善性能, such as 添加多个元素到同一个list中.

Spring Data Redis 提供几个 RedisTemplate 的方法支持在一个 pipeline(管道) 中执行多个指令.若是不关注管道操做的结果, 能够使用标准的execute方法, 传递true 的pipeline参数.

executePipelined 方法会执行 RedisCallback or SessionCallback 的回调方法以返回结果.

//pop a specified number of items from a queue
List results = stringRedisTemplate.executePipelined(new RedisCallback() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
for(int i=0; i< batchSize; i++) { stringRedisConn.rPop(“myqueue”); } return null; } });

在redis官网中: ( 官网地址: https://redis.io/topics/pipelining )

从 It’s not just a matter of RTT 这一段开始, pipeline不单单是不用等待回复时间(RTT)就能够持续的发送指令. 而且使用 pipeline的时候, redis用一个read()操做读取多个指令,而且 经过一个 write() 传递多个结果.最终的结果, 使用 pipeline 的效率甚至能至关于不使用pipeline的 10 倍.

(来张官网的图)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号