Basic Tutorials of Redis(8) -Transaction

2023-06-05,,

  Data play an important part in our project,how can we ensure correctness of the data and prevent the data

from error.In relational database, we are famillar with the usage of transaction.

begin
opreations
commit/rollback

  But there are some differences between the Redis and relational database.Let's introduce the commands

first.There are only 5 commands to use while you use the transaction in redis.

  I want to take an example to show you how to use the transaction.So here is the backgroud.There are three

persons and they want to buy the apple from the shop.The key for buying apple is the amount.

  For this example,I use the hash to store the infomation of the users and the products.I use the string to store

the totalmoney for the trade.The following picture shows this example's initial data.

  Let's start to finish this example.The first step is to initialize the data.

hmset user- name tom money
hmset user- name jack money
hmset user- name paul money
hmset product- name apple price amount
set totalmoney

  Tom wants to buy the apple,so the product-1's amount should decrease by one,tom's money should decrease

by 150 and the totalmoney should increase by 150.The action buy includes three steps.We should check up the

product-1 with buy action.

watch product-
multi
hincrby product- amount -
hincrby user- money -
incrby totalmoney
exec

  You will find that after the command  multi ,the commands before  exec  all return queued.It means that those

commands store in a queue.After exec command,the client send this queue to the server and execute the commands.

But this is a normal situation that the amount is not changed before the transaction execute.

  Now jack want to buy the apple , so does paul.And paul takes first,so jack can't buy the apple successfully!To show

this,we need another client to imitate paul takes first.Before execute the transaction of jack's action,we decrease the

amount of product-1 to imitate paul buy the apple first.After executing the transaction of jack's action,the client returns

(nil) meaning the transaction of jack's action executed fail and the three commands didn't execute too.

  This example shows the basic usage of transaction in redis.It is suitable for many questions.

  command  discard can help you to abort the current transaction.After executing the command discard.At this time

execute the command exec will return an error.Because the multi didn't exists in this session.

  Now I will show you how to use the transaction in StackExchange.Redis.

              //Initiate the data
db.HashSet("user-1", new HashEntry[] { new HashEntry("name", "tom"), new HashEntry("money", ) });
db.HashSet("user-2", new HashEntry[] { new HashEntry("name", "jack"), new HashEntry("money", ) });
db.HashSet("user-3", new HashEntry[] { new HashEntry("name", "paul"), new HashEntry("money", ) });
db.HashSet("product-1", new HashEntry[] { new HashEntry("name", "apple"), new HashEntry("price", ),new HashEntry("amount", ) });
db.StringSet("totalmoney", ); //multi
var tran = db.CreateTransaction();
//watch
tran.AddCondition(Condition.HashEqual("product-1", "amount", "")); tran.HashDecrementAsync("product-1", "amount");
tran.HashDecrementAsync("user-1", "money", );
tran.StringIncrementAsync("totalmoney", ); Console.WriteLine(string.Format("execute the transaction {0}!", tran.Execute() ? "Y" : "N")); //multi
var tran2 = db.CreateTransaction();
//watch
tran.AddCondition(Condition.HashEqual("product-1", "amount", "")); tran.HashDecrementAsync("product-1", "amount");
tran.HashDecrementAsync("user-2", "money", );
tran.StringIncrementAsync("totalmoney", ); //paul take first
db.HashDecrement("product-1", "amount"); Console.WriteLine(string.Format("execute the transaction {0}!", tran.Execute() ? "Y" : "N"));

  Debuging the code ,you will see the result as follow.

  

  The next post of this series is the RedisHelper.Thanks for your reading
 

Basic Tutorials of Redis(8) -Transaction的相关教程结束。

《Basic Tutorials of Redis(8) -Transaction.doc》

下载本文的Word格式文档,以方便收藏与打印。