springboot 的 RedisTemplate 的 execute 和 executePipelined 功能的区别redis
1.executespring
如下是 springboot 官網(wǎng)原文: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.ideSpring 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<List>() {
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 經(jīng)過multi, exec, discard 操做提供事務(wù)支持. RedisTemplate 也一樣支持這些操做, 然而 RedisTemplate 不保證在同一個鏈接中執(zhí)行事務(wù)中的全部操做.orm
當(dāng)使用 redis 的事務(wù)的時候, Spring Data Redis 提供 SessionCallback 的接口支持多個操做的執(zhí)行都在同一個鏈接中.
2.Pipeline
Redis provides support for pipelining, which involves sending multiple commands to the server without waitingfor 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(管道) 的支持, 它能夠發(fā)送多條指令到 redis服務(wù)端 而不用等待服務(wù)端的回復(fù) 而且 讀取服務(wù)端的回復(fù)在一步操做中. 當(dāng)你須要連續(xù)發(fā)送多條命令的時候 Pipelining(管道) 能夠改善性能, such as 添加多個元素到同一個list中.
Spring Data Redis 提供幾個 RedisTemplate 的方法支持在一個 pipeline(管道) 中執(zhí)行多個指令.若是不關(guān)注管道操做的結(jié)果, 能夠使用標(biāo)準(zhǔn)的execute方法, 傳遞true 的pipeline參數(shù).
executePipelined 方法會執(zhí)行 RedisCallback or SessionCallback 的回調(diào)方法以返回結(jié)果.
//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官網(wǎng)中: ( 官網(wǎng)地址: https://redis.io/topics/pipelining )
從 It’s not just a matter of RTT 這一段開始, pipeline不單單是不用等待回復(fù)時間(RTT)就能夠持續(xù)的發(fā)送指令. 而且使用 pipeline的時候, redis用一個read()操做讀取多個指令,而且 經(jīng)過一個 write() 傳遞多個結(jié)果.最終的結(jié)果, 使用 pipeline 的效率甚至能至關(guān)于不使用pipeline的 10 倍.
(來張官網(wǎng)的圖)
總結(jié)
以上是生活随笔為你收集整理的springboot 的 RedisTemplate 的 execute 和 executePipelined 功能的区别redis的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IJCAI 2021 ICAPS 20
- 下一篇: MySQL数据库的数据类型以及取值范围详