博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Basic Tutorials of Redis(8) -Transaction
阅读量:5860 次
发布时间:2019-06-19

本文共 4362 字,大约阅读时间需要 14 分钟。

  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.

beginopreationscommit/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-1 name tom money 200hmset user-2 name jack money 300hmset user-3 name paul  money 500hmset product-1 name apple price 150 amount 2set totalmoney 0

 

  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-1multihincrby product-1 amount -1hincrby user-1 money -150incrby totalmoney 150exec

 

  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.

1              //Initiate the data 2             db.HashSet("user-1", new HashEntry[2] { new HashEntry("name", "tom"), new HashEntry("money", 200) }); 3             db.HashSet("user-2", new HashEntry[2] { new HashEntry("name", "jack"), new HashEntry("money", 300) }); 4             db.HashSet("user-3", new HashEntry[2] { new HashEntry("name", "paul"), new HashEntry("money", 500) }); 5             db.HashSet("product-1", new HashEntry[3] { new HashEntry("name", "apple"), new HashEntry("price", 150),new HashEntry("amount", 2) }); 6             db.StringSet("totalmoney", 0); 7              8             //multi 9             var tran = db.CreateTransaction();10             //watch11             tran.AddCondition(Condition.HashEqual("product-1", "amount", "2"));12 13             tran.HashDecrementAsync("product-1", "amount");14             tran.HashDecrementAsync("user-1", "money", 150);15             tran.StringIncrementAsync("totalmoney", 150);16 17             Console.WriteLine(string.Format("execute the transaction {0}!", tran.Execute() ? "Y" : "N"));18 19             //multi20             var tran2 = db.CreateTransaction();21             //watch22             tran.AddCondition(Condition.HashEqual("product-1", "amount", "1"));23 24             tran.HashDecrementAsync("product-1", "amount");25             tran.HashDecrementAsync("user-2", "money", 150);26             tran.StringIncrementAsync("totalmoney", 150);27 28             //paul take first29             db.HashDecrement("product-1", "amount");30 31             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
 

转载于:https://www.cnblogs.com/catcher1994/p/5861118.html

你可能感兴趣的文章
BCH测试网上出现第一个UTXO证明
查看>>
oracle 11G R2 安装
查看>>
5月14日期待与大家见面!推广51CTO技术沙龙第五期:IT技术人茫茫职业人生路
查看>>
php 直接调用svn命令
查看>>
30. C# -- 反射(2)
查看>>
Axis2调用WebService
查看>>
接口测试总结
查看>>
平台建成-庆祝一下
查看>>
mac 为finder添加 新建文本文档
查看>>
Python基础(list类)
查看>>
Android实现简单的电子词典
查看>>
Lesson 9
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
PHP排序算法的复习和总结
查看>>
Set the Round Robin IOPS limit from 1000 to 1
查看>>
思想:gzcompress和gzdeflate的区别(15)
查看>>
产品管理:启示录 - 特约客户、产品验证、原型测试
查看>>
IP地址库ipip.net
查看>>
Java 数据类型定义
查看>>