Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of transaction Operation in redis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of transaction operation in redis, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Transactions between redis and mysql

Redis supports simple transactions

Easy to use

Transfer Zhang San's 100RMB money to lisi:

Set zhangsan 800set lisi 100multidecrby zhangsan 100incrby lisi 100exec

Two cases of failure

In the statements that follow the mutil, there are two possible cases of statement errors, which are analyzed in terms of money transfer:

(1) there is a problem with grammar.

127.0.1 hasdfasdf 6379 > multiOK127.0.0.1:6379 > decrby zhang 100QUEUED127.0.0.1:6379 > hasdfasdf (error) ERR unknown command 'hasdfasdf'127.0.0.1:6379 > exec (error) EXECABORT Transaction discarded because of previous errors.127.0.0.1:6379 > mget zhang wang1) "800" 2) "100"

In this case, in exec, an error is reported, and all statements cannot be executed, so it is still 800 and 100 yuan.

(2) the grammar itself is correct, but there is something wrong with the object of application.

127.0.1 multiOK127.0.0.1:6379 > decrby zhang 100QUEUED127.0.0.1:6379 > sadd wang 1QUEUED127.0.0.1:6379 > exec1) (integer) 7002) (error) WRONGTYPE Operation against a key holding the wrong kind of value127.0.0.1:6379 > mget zhang wang1) "700" 2) "100"

After Exec, the correct statement will be executed and inappropriate statements will be skipped, so here is a 700yen and a 100yen

Case

Suppose that in the case of buying a ticket, there is currently only one ticket and 100RMB. If I am in the process of buying a ticket, after my multi, and before exec, the ticket is bought by someone else-that is, the ticket has become zero, and then we will change the ticket to-1 when we execute exec. This is not right.

127.0.1 set ticket 1OK127.0.0.1:6379 > set money 100OK127.0.0.1:6379 > multiOK127.0.0.1:6379 > decr ticketQUEUED127.0.0.1:6379 > decrby money 10QUEUED127.0.0.1:6379 > exec1) (integer)-12) (integer) 90

Use watch to check whether the ticket has been bought

Use watch to detect the specified key, which is responsible for monitoring that the key has not been changed.

127.0.0.1 watch ticketOK127.0.0.1:6379 6379 > set ticket 1OK127.0.0.1:6379 > set money 100OK127.0.0.1:6379 > watch ticketOK127.0.0.1:6379 > multiOK127.0.0.1:6379 > decr ticketQUEUED127.0.0.1:6379 > decrby money 10QUEUED127.0.0.1:6379 > exec (nil) / / returns nil, indicating that the monitored ticket has changed and the transaction is cancelled. 127.0.0.1: 100 > mget ticket money1) "0" 2) "100"

Before executing exec, the ticket is bought and the ticket is 0, and then after executing exec, it is found that ticket is passive, so the transaction is not executed and the transaction is cancelled. Returns nil when exec is executed.

Related usage of watch

Watch key1 key2... KeyN

Function: monitor whether the key1 key2..keyN has changed. If so, the transaction will be canceled.

Unwatch thank you for reading this article carefully. I hope the article "sample Analysis of transaction Operations in redis" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report