加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Basic Tutorials of Redis(8) -Transaction

发布时间:2020-12-16 04:47:04 所属栏目:安全 来源:网络整理
导读: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. /rollback But there are some differences between the

  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.

/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?

example's initial data.

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

hmset user- name tom money - name jack money - name paul money - name apple price amount 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-- amount -- money -

  You will find that after the command ??,the commands before ??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.

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?

and the three commands didn't execute too.

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

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

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

db.HashSet(, HashEntry[] { HashEntry(,), HashEntry(, db.HashSet(,), db.HashSet(,), db.HashSet(, HashEntry[] { HashEntry(,), HashEntry(,), HashEntry(, db.StringSet(, tran = tran.AddCondition(Condition.HashEqual(,, tran.HashDecrementAsync(, tran.HashDecrementAsync(,, tran.StringIncrementAsync(, Console.WriteLine(.Format(,tran.Execute() ? : tran2 = tran.AddCondition(Condition.HashEqual(, tran.HashDecrementAsync(, tran.HashDecrementAsync(, tran.StringIncrementAsync(, db.HashDecrement(, Console.WriteLine(.Format(,tran.Execute() ? : ));

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

  

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读