Redis 管道 Pipeline

Redis 管道 Pipeline

JAVA代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 管道,批量操作
*
* @author zc
* @date 2020/9/15 18:46
*/
@Service
public class PipelineService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;

public void pipeline() {
redisTemplate.executePipelined((RedisCallback<String>) connection -> {
for (int i = 0; i < 100; i++) {
connection.set(("pipeline:" + i).getBytes(), "123".getBytes());
}
return null;
});
}

public void common() {
for (int i = 0; i < 100; i++) {
redisTemplate.opsForValue().set("common:" + i, "123");
}
}
}

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@SpringBootTest
class RedisPipelineApplicationTests {

@Autowired
PipelineService pipelineService;

/**
* 测试管道和普通的速度
* pipeline: 272ms
* common: 6859ms
*/
@Test
void redisPipeline() {
long t1 = System.currentTimeMillis();
pipelineService.pipeline();
long t2 = System.currentTimeMillis();
pipelineService.common();
long t3 = System.currentTimeMillis();
System.out.println("pipeline: " + (t2 - t1) + "ms");
System.out.println("common: " + (t3 - t2) + "ms");
}
}

Redis 管道 Pipeline
https://happyloves.cn/20220831/8ef2129f9c66.html
作者
赵小胖
发布于
2022年8月31日
许可协议