Home > Database > Redis > About Redis transactions and pipeline

About Redis transactions and pipeline

藏色散人
Release: 2020-11-05 16:04:03
forward
2619 people have browsed it

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);
Copy after login
<br>
Copy after login

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();
Copy after login

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>
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template