Related expansion of Redis (transaction, monitoring, Jedis)

1. Affairs

1 Introduction

The nature of Redis transactions: a set of commands, all commands in a transaction will be serialized, and will be executed in order during transaction execution. One-time, sequential, and exclusive execution of a series of commands.
Redis transactions do not have the concept of isolation level
. All commands are not directly executed in the transaction. They will only be executed when the execution command is initiated.
Redis single command saves atomicity , but transactions do not guarantee atomicity.

2. Process

Open transaction (multi)
command enqueue (...)
execute transaction (exec)

3. shell command

3.1 Start a transaction

# 开启事务
127.0.0.1:6379> multi
OK
# 命令入队
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> get k2
QUEUED
127.0.0.1:6379(TX)> set k3 v3
QUEUED
# 执行事务
127.0.0.1:6379(TX)> exec
1) OK
2) OK
3) "v2"
4) OK

3.2 Abandoning a transaction

None of the commands in the transaction queue will be executed

# 开启事务
127.0.0.1:6379> multi
OK
# 命令入队
127.0.0.1:6379(TX)> set k1 v1
QUEUED
127.0.0.1:6379(TX)> set k2 v2
QUEUED
127.0.0.1:6379(TX)> get k2
QUEUED
127.0.0.1:6379(TX)> set k3 v3
QUEUED
# 放弃事务
127.0.0.1:6379(TX)> discard
ok

3.3 Compilation exceptions (the code is wrong, the command is wrong)

All commands in the transaction will not be executed

3.4 Runtime exceptions

If there is a grammatical error in the transaction queue, when the command is executed, other commands can be executed normally, and the wrong command throws an exception.

Two, monitoring (watch)

1. lock

1.1 Pessimistic lock

Very pessimistic, thinking that there will be problems at any time, and everything will be locked.

1.2 Optimistic lock

Very optimistic, thinking that there will be no problems at any time, so it will not be locked. When updating the data, judge whether someone has modified the data during this period.
Compare version when getting version
update

127.0.0.1:6379> set money 100
OK
127.0.0.1:6379> set out 0
OK
# 监视money对象(加乐观锁)
127.0.0.1:6379> watch money
OK
# 事务正常结束,数据期间没有发生变动,这个时候就正常执行成功
127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> decrby money 20
QUEUED
127.0.0.1:6379(TX)> incrby out 20
QUEUED
127.0.0.1:6379(TX)> exec
1) (integer) 80
2) (integer) 20

2. Pay attention

2.1 Principle

When using multi-thread to modify the value, using watch can be used as an optimistic lock operation of redis,
that is, before executing this thread (A), another thread (B) modifies the value of the monitored object (C). At this time, if this Thread (A) continues to perform some operations, which will cause the transaction to fail. Unless the thread (A) first gives up monitoring the object (C), and then re-monitors the object (C), after performing some operations, the transaction can be executed successfully.

2.2 Process

(1) If it is found that the transaction execution fails, it will be unlocked first

unwatch

(2) Then get the new value and monitor again

watch money

(3) Check whether the monitored value has changed. If there is no change, the execution can be successful, and if there is a change, the execution will fail.

exec

3. Jedis

1 Introduction

Jedis is a Java-based redis client, which integrates redis command operations and provides connection pool management.

2. Dependence

2.1 Main dependencies

Used for redis connection

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<!-- jedis库 -->
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>4.2.3</version>
</dependency>

2.2 Secondary dependencies

Used for json operations

<!--HUTool工具-->
<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.8.9</version>
</dependency>

2. Sample

@Test
void contextLoads() {
    
    
    // 1、new jedis对象
    Jedis jedis = new Jedis("127.0.0.1",6379);
    System.out.println(jedis.ping());
}

result
insert image description here

3. Commands related to basic operations

Since the previous blog wrote a very complete command, Redis's 2022 most complete knowledge of the five basic data types and shell commands,
so the following is a rough outline of the operation of jedis, just for understanding

@Test
void testAll(){
    
    
    // 1、new jedis对象
    Jedis jedis = new Jedis("127.0.0.1",6379);
    System.out.println("清空数据:" + jedis.flushDB());
    System.out.println("新增键值对:" + jedis.set("name", "zhangsan"));
    System.out.println("判断某个数据是否存在:" + jedis.exists("name"));
    System.out.println("查看系统中所有的键:");
    Set<String> keys = jedis.keys("*");
    System.out.println(keys);
    System.out.println("删除键:" + jedis.del("name"));
}

result
insert image description here

4. Transaction-related operations

@Test
void test2(){
    
    
    // 1、new jedis对象
    Jedis jedis = new Jedis("124.223.218.196",6379);
    jedis.flushDB();
    JSONObject jsonObject = new JSONObject();
    jsonObject.set("hello", "world");
    jsonObject.set("name", "zhangsan");

    // 开启事务
    Transaction multi = jedis.multi();
    String result = jsonObject.toString();

    try{
    
    
        multi.set("user1", result);
        multi.set("user2", result);
        // 执行事务
        multi.exec();
    }catch (Exception e) {
    
    
        // 放弃事务
        multi.discard();
        e.printStackTrace();
    }finally {
    
    
        System.out.println(jedis.get("user1"));
        System.out.println(jedis.get("user2"));
        // 关闭连接
        jedis.close();
    }
}

result
insert image description here

Guess you like

Origin blog.csdn.net/qq_46106857/article/details/128335211