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.
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
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.
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.
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 irst.After
and the three commands didn't execute too.
This example shows the basic usage of transaction in redis.It is suitable for many questions.
Now I will show you how to use the transaction in StackExchange.Redis.
Debuging the code,you will see the result as follow.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。