Home  >  Article  >  Database  >  About Redis transactions and pipeline

About Redis transactions and pipeline

藏色散人
藏色散人forward
2020-11-05 16:04:032567browse

The following column Redis Tutorial will introduce you to Redis transactions and pipeline. I hope it will be helpful to friends in need!

About Redis transactions and pipeline

1. reidis transaction

Redis transaction can execute multiple commands at one time, and has the following three important Guarantee:

  • Batch operations are put into the queue cache before sending the EXEC command.
  • Enter transaction execution after receiving the EXEC command. If any command in the transaction fails to execute, the remaining commands are still executed.
  • During the transaction execution process, command requests submitted by other clients will not be inserted into the transaction execution command sequence.

A transaction will go through the following three stages from start to execution:

  • Start transaction.
  • Command to join the queue.
  • Execute transactions.

MULTI Start a transaction, then queue multiple commands into the transaction, and finally the transaction is triggered by the EXEC command, Execute all commands in the transaction together:

1. Transaction execution

About Redis transactions and pipeline

2. watch

Monitor one (or more) keys. If this (or these) keys are modified by other commands before the transaction is executed, the transaction will be interrupted.

About Redis transactions and pipelineAbout Redis transactions and pipeline

3. discard

Cancel the transaction and give up execution All commands within the transaction block.

About Redis transactions and pipeline

2. jedis code

1. Configuration support Transaction

template.setEnableTransactionSupport(true);
<br>

2. Code:

<br>##

 redisTemplate.opsForValue().set("aaa", 321);
redisTemplate.watch("aaa");
redisTemplate.multi();
redisTemplate.opsForValue().set("aaa", 123);
redisTemplate.opsForValue().set("bbb", 123);
redisTemplate.exec();

3.pipleline

jedis Code:

<br>

Map<string> map = new HashMap();
map.put("aaa", 111);
map.put("bbb", 222);
map.put("ccc", 3333);

List list = redisTemplate.executePipelined(new RedisCallback<object>() {
    @Override
    public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
        redisConnection.openPipeline();        for (Map.Entry<string> mapEntry : map.entrySet()) {
            redisConnection.set(redisTemplate.getKeySerializer().serialize(mapEntry.getKey()), redisTemplate.getValueSerializer().serialize(mapEntry.getValue()));
        }        return null;
    }
}, redisTemplate.getValueSerializer());

System.out.println(redisUtil.get("aaa"));
System.out.println(redisUtil.get("bbb"));
System.out.println(redisUtil.get("ccc"));</string></object></string>

The above is the detailed content of About Redis transactions and pipeline. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What is redis slow queryNext article:What is redis slow query