Basic Tutorials of Redis(8) -Transaction
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.
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.
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.
Basic Tutorials of Redis(8) -Transaction的更多相关文章
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
- JDBC Tutorials: Commit or Rollback transaction in finally block
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...
随机推荐
- 史上最详细git教程
题外话 虽然这个标题很惊悚,不过还是把你骗进来了,哈哈-各位看官不要着急,耐心往下看 Git是什么 Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别 SVN是集中式版本控制 ...
- [APUE]标准IO库(下)
一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- CRL快速开发框架系列教程九(导入/导出数据)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- javascript运动系列第一篇——匀速运动
× 目录 [1]简单运动 [2]定时器管理 [3]分享到效果[4]移入移出[5]运动函数[6]透明度[7]多值[8]多物体[9]回调[10]函数完善[11]最终函数 前面的话 除了拖拽以外,运动也是j ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- isEmpty和isNull()区别
isEmpty和isNull()区别一个NULL字符串一定是一个空串,一个空串未必是一个NULL字符串例如:QString().isNull(): //结果为trueQString().isEm ...
- SharePoint2016安装的过程的”Microsoft.SharePoint.Upgrade.SPUpgradeException”错误解决方法
前提 在windows server 2012的服务器上运行安装sharepoint2016出现如下错误: Could not load file or assembly ‘Microsoft.Dat ...
- Android Studio —— 创建Menu菜单项
大多数android程序的右上角都会设置一个菜单按钮比如微信的界面右上角的加号. 这个需要在layout同级目录下新建文件夹命名为menu,再右击新建的menu新建xml文件:
- Android开发案例 – 在AbsListView中使用倒计时
在App中, 有多种多样的倒计时需求, 比如: 在单View上, 使用倒计时, 如(如图-1) 在ListView(或者GridView)的ItemView上, 使用倒计时(如图-2) 图-1 图-2 ...