本文共 6705 字,大约阅读时间需要 22 分钟。
redis中的事务
is an open-source, in-memory key-value data store. Redis allows you to plan a sequence of commands and run them one after another, a procedure known as a transaction. Each transaction is treated as an uninterrupted and isolated operation, which ensures data integrity. Clients cannot run commands while a transaction block is being executed
是一个开源的内存中键值数据存储。 Redis允许您计划一系列命令,然后一个接一个地运行它们,这一过程称为transaction 。 每个事务都被视为不间断且隔离的操作,以确保数据完整性。 客户端在执行事务块时无法运行命令
This tutorial goes over how to execute and cancel transactions, and also includes some information on pitfalls commonly associated with transactions.
本教程介绍了如何执行和取消交易,还包括一些与交易通常相关的陷阱的信息。
This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.
本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。
The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on . We will demonstrate how these commands behave by running them with redis-cli
, the Redis command line interface. Note that if you’re using a different Redis interface — , for example — the exact output of certain commands may differ.
本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli
运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如 ,则某些命令的确切输出可能会有所不同。
Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our . Then, you must either or in order to connect to the Managed Database over TLS.
另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的 。 然后, 您必须 或 才能通过TLS连接到托管数据库。
The multi
command tells Redis to begin a transaction block. Any subsequent commands will be queued up until you run an exec
command, which will execute them.
multi
命令告诉Redis开始事务块。 在运行exec
命令之前,所有后续命令都将排队等待执行。
The following commands form a single transaction block. The first command initiates the transaction, the second sets a key holding a string with the value of 1
, the third increases the value by 1, the fourth increases its value by 40, the fifth returns the current value of the string, and the last one executes the transaction block:
以下命令形成一个事务块。 第一个命令启动事务,第二个命令设置一个键,该键保存一个值为1
的字符串,第三个命令将该值增加1,第四个命令将其值增加40,第五个返回该字符串的当前值,最后一个返回一个执行事务块:
After running multi
, redis-cli
will respond to each of the following commands with QUEUED
. After you run the exec
command, it will show the output of each of those commands individually:
运行multi
, redis-cli
将使用QUEUED
响应以下每个命令。 运行exec
命令后,它将分别显示每个命令的输出:
Output 1) OK2) (integer) 23) (integer) 424) "42"
Commands included in a transaction block are run sequentially in the order they’re queued. Redis transactions are atomic, meaning that either every command in a transaction block is processed (meaning that it’s accepted as valid and queued to be executed) or none are. However, even if a command is successfully queued, it may still produce an error when executed. In such cases, the other commands in the transaction can still run, but Redis will skip the error-causing command. See the section on for more details.
事务块中包含的命令按排队顺序依次运行。 Redis事务是原子的 ,这意味着要么处理事务块中的每个命令(意味着它被视为有效命令并排队等待执行),要么不执行。 但是,即使命令成功排队,执行时它仍然可能产生错误。 在这种情况下,事务中的其他命令仍然可以运行,但是Redis将跳过导致错误的命令。 有关更多详细信息,请参见的部分。
To cancel a transaction, run the discard
command. This prevents any previously-queued commands from running:
要取消交易,请运行discard
命令。 这样可以防止任何先前排队的命令运行:
Output OK
The discard
command returns the connection to a normal state, which tells Redis to run single commands as usual. You’ll need to run multi
again to tell the server you’re starting another transaction.
discard
命令将连接返回到正常状态,该状态告诉Redis像往常一样运行单个命令。 您需要再次运行multi
以告知服务器您正在开始另一笔交易。
Some commands may be impossible to queue, such as commands with syntax errors. If you attempt to queue a syntactically incorrect command Redis will return an error.
某些命令可能无法排队,例如语法错误的命令。 如果尝试对语法错误的命令进行排队,则Redis将返回错误。
The following transaction createst a key named key_A
and then attempts to increment it by 10. However, a spelling error in the incrby
command causes and error and closes the transaction:
以下事务创建了一个名为key_A
的键,然后尝试将其递增10。但是, incrby
命令中的拼写错误会导致并出错并关闭事务:
Output (error) ERR unknown command 'incrbuy'
If you try to run an exec
command after trying to queue a command with a syntax error like this one, you will receive another error message telling you that the transaction was discarded:
如果您尝试在将命令与类似语法错误的命令exec
队列后尝试运行exec
命令,则会收到另一条错误消息,告诉您该事务已被丢弃:
Output (error) EXECABORT Transaction discarded because of previous errors.
In cases like this, you’ll need to restart the transaction block and make sure you enter each command correctly.
在这种情况下,您需要重新启动事务块并确保正确输入每个命令。
Some impossible commands are possible to queue, such as running incr
on a key containing only a string. Because such command is syntactically correct, Redis won’t return an error if you try to include it in a transaction and won’t prevent you from running exec
. In cases like this, all other commands in the queue will be executed, but the impossible command will return an error:
一些不可能命令是可能的队列,例如运行incr
上仅含有一个字符串的键。 因为这样的命令在语法上是正确的,所以如果您尝试将它包含在事务中,则Redis不会返回错误,也不会阻止您运行exec
。 在这种情况下,将执行队列中的所有其他命令,但不可能的命令将返回错误:
Output 1) OK2) (error) ERR value is not an integer or out of range
For more information on how Redis handles errors inside transactions, see the .
有关Redis如何处理事务中的错误的更多信息,请参阅的 。
This guide details a number of commands used to create, run, and cancel transactions in Redis. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.
本指南详细介绍了许多用于在Redis中创建,运行和取消事务的命令。 如果您想在本指南中概述其他相关的命令,参数或过程,请在下面的注释中提出疑问或提出建议。
For more information on Redis commands, see our tutorial series on .
有关Redis命令的更多信息,请参阅关于系列教程。
翻译自:
redis中的事务
转载地址:http://ivhgb.baihongyu.com/