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.

  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.

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

  

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

Basic Tutorials of Redis(8) -Transaction的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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#? ...

  8. 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 ...

  9. JDBC Tutorials: Commit or Rollback transaction in finally block

    http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...

随机推荐

  1. 使用RequireJS并实现一个自己的模块加载器 (一)

    RequireJS & SeaJS 在 模块化开发 开发以前,都是直接在页面上引入 script 标签来引用脚本的,当项目变得比较复杂,就会带来很多问题. JS项目中的依赖只有通过引入JS的顺 ...

  2. 从啥也不会到可以胜任最基本的JavaWeb工作,推荐给新人的学习路线(二)

    在上一节中,主要阐述了JavaScript方面的学习路线.先列举一下我朋友的经历,他去过培训机构,说是4个月后月薪过万,虽然他现在还未达到这个指标. 培训机构一般的套路是这样:先教JavaSE,什么都 ...

  3. PHP设计模式(八)桥接模式(Bridge For PHP)

    一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 二.案例 1.模拟毛笔(转) 需求:现在需要准备三种粗细(大中小),并且有五种颜色的比 如果使用蜡 ...

  4. 玩转ajax

    1.什么是ajax? Ajax 是 Asynchronous JavaScript and XML(以及 DHTML 等)的缩写. 2.ajax需要什么基础? HTML 用于建立 Web 表单并确定应 ...

  5. Mysql 学习之基础操作

    一.表复制 1.复制表结构    将表hello的结构复制一份为表hello3 2.复制数据 a.如果两张表的结构一样且你要复制所有列的数据 mysql> insert into hello3 ...

  6. BZOJ 1006 【HNOI2008】 神奇的国度

    题目链接:神奇的国度 一篇论文题--神奇的弦图,神奇的MCS-- 感觉我没有什么需要多说的,这里简单介绍一下MCS: 我们给每个点记录一个权值,从后往前依次确定完美消除序列中的点,每次选择权值最大的一 ...

  7. Linux.NET学习手记(6)

    各位读者大家好,好长一段时间没有更新文章了,自从参加工作之后,每天等待去做的工作没完没了,个人的时间也变得奢侈起来,今后要尽量从中脱身,抽更多的时间来完成自己想做的事情(希望如此). 言归正传,上一回 ...

  8. C#中一些常用的加密和哈希处理

    URL编码,默认UTF8编码方式 /// <summary> /// URL编码,默认UTF8编码方式 /// </summary> /// <param name=&q ...

  9. BIO\NIO\AIO记录

    IO操作可以分为3类:同步阻塞(BIO).同步非阻塞(NIO).异步(AIO). 同步阻塞(BIO):在此种方式下,用户线程发起一个IO操作以后,必须等待IO操作的完成,只有当真正完成了IO操作以后, ...

  10. Nodejs之MEAN栈开发(六)---- 用Angular创建单页应用(上)

    在上一节中我们学会了如何在页面中添加一个组件以及一些基本的Angular知识,而这一节将用Angular来创建一个单页应用(SPA).这意味着,取代我们之前用Express在服务端运行整个网站逻辑的方 ...