Redis 简明教程

Redis - Transactions

Redis 事务允许在一步中执行一组命令。以下是事务的两个特性。

Redis transactions allow the execution of a group of commands in a single step. Following are the two properties of Transactions.

  1. All commands in a transaction are sequentially executed as a single isolated operation. It is not possible that a request issued by another client is served in the middle of the execution of a Redis transaction.

  2. Redis transaction is also atomic. Atomic means either all of the commands or none are processed.

Sample

Redis 事务由命令 MULTI 启动,然后您需要传递一个应作为事务执行的命令列表,之后整个事务将由命令 EXEC 执行。

Redis transaction is initiated by command MULTI and then you need to pass a list of commands that should be executed in the transaction, after which the entire transaction is executed by EXEC command.

redis 127.0.0.1:6379> MULTI
OK
List of commands here
redis 127.0.0.1:6379> EXEC

Example

以下示例说明了如何启动和执行 Redis 事务。

Following example explains how Redis transaction can be initiated and executed.

redis 127.0.0.1:6379> MULTI
OK
redis 127.0.0.1:6379> SET tutorial redis
QUEUED
redis 127.0.0.1:6379> GET tutorial
QUEUED
redis 127.0.0.1:6379> INCR visitors
QUEUED
redis 127.0.0.1:6379> EXEC
1) OK
2) "redis"
3) (integer) 1

Redis Transaction Commands

下表显示了与 Redis 事务相关的一些基本命令。

Following table shows some basic commands related to Redis transactions.

Sr.No

Command & Description

1

DISCARDDiscards all commands issued after MULTI

2

EXECExecutes all commands issued after MULTI

3

MULTIMarks the start of a transaction block

4

UNWATCHForgets about all watched keys

5

WATCH key [key …​]Watches the given keys to determine the execution of the MULTI/EXEC block