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 ...
随机推荐
- c#语言规范
0x00 分类 C#语言规范主要有两个来源,即我们熟知的ECMA规范和微软的规范.尽管C#的ECMA规范已经前后修订4次,但其内容仅仅到C# 2.0为止.所以慕容为了方便自己和各位方便查询,在此将常见 ...
- javascript函数
array.sort(function(a, b){ return a -b ; } ) 把数组 array 按照从小到大排序. [11, 22, 586, 10, -58, 86].sort(f ...
- WebLogic的安装和配置以及MyEclipse中配置WebLogic
WebLogic 中间件: 是基础软件的一大类,属于可复用软件的范畴,顾名思义,中间件属于操作系统软件与应用软件的中间,比如:JDK,框架,weblogic. weblogic与tomcat区别 : ...
- Linux设备管理(四)_从sysfs回到ktype
sysfs是一个基于ramfs的文件系统,在2.6内核开始引入,用来导出内核对象(kernel object)的数据.属性到用户空间.与同样用于查看内核数据的proc不同,sysfs只关心具有层次结构 ...
- 吸顶大法 -- UWP中的工具栏吸顶的实现方式之一
如果一个页面中有很长的列表/内容,很多应用都会在用户向下滚动时隐藏页面的头,给用户留出更多的阅读空间,同时提供一个方便的吸顶工具栏,比如淘宝中的店铺页面. 下面是一个比较简单的实现,如果有同学有更好的 ...
- EasyPR--开发详解(8)文字定位
今天我们来介绍车牌定位中的一种新方法--文字定位方法(MSER),包括其主要设计思想与实现.接着我们会介绍一下EasyPR v1.5-beta版本中带来的几项改动. 一. 文字定位法 在EasyPR前 ...
- [Intel Edison开发板] 05、Edison开发基于MRAA实现IO控制,特别是UART通信
一.前言 下面是本系列文章的前几篇: [Intel Edison开发板] 01.Edison开发板性能简述 [Intel Edison开发板] 02.Edison开发板入门 [Intel Edison ...
- Android 浏览器 —— 使用 WebView 实现文件下载
对当前的WebView设置下载监听 mCurrentWebView.setDownloadListener(new DownloadListener() { @Override public void ...
- Redis数据结构详解之Set(三)
序言 在Redis中,Set和list都是字符串序列,非常相似,不同之处在于Set是用哈希表来保持字符串的唯一性,没有先后顺序,不像list一样,可以在首尾增删数据.但是set也有自己的相应处理命令来 ...
- 【.NET深呼吸】(WPF)跨窗口完成绑定
在99.999975%的情形下,可视化元素之间的相互绑定都会在同一个容器中完成,如同一个窗口,同一个UserControl等.但在极少数的情况下,为了从最大限度减少代码赋值的前提下考虑,是可以使用跨窗 ...