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 ...
随机推荐
- .NET Core的日志[3]:将日志写入Debug窗口
定义在NuGet包"Microsoft.Extensions.Logging.Debug"中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志 ...
- UWP开发之Mvvmlight实践九:基于MVVM的项目架构分享
在前几章介绍了不少MVVM以及Mvvmlight实例,那实际企业开发中将以那种架构开发比较好?怎样分层开发才能节省成本? 本文特别分享实际企业项目开发中使用过的项目架构,欢迎参照使用!有不好的地方欢迎 ...
- XSS分析及预防
XSS(Cross Site Scripting),又称跨站脚本,XSS的重点不在于跨站点,而是在于脚本的执行.在WEB前端应用日益发展的今天,XSS漏洞尤其容易被开发人员忽视,最终可能造成对个人信息 ...
- required
required,这是HTML5中的一个新属性:这是HTML5中input元素中的一个属性. required译为必须的,在input元素中应用这一属性,就表示这一input元素节点是必填的或者必选的 ...
- BPM应用开发解决方案分享
一.需求分析企业整体管理是一个完整的体系,如果 把这个体系比做一个拼图,企业信息化通过各个业务系统覆盖了一部分业务. 企业通过采购实施通用软件的方式,覆盖了企业的核心业务和专业化业务然而系统只满足了部 ...
- 【SAP业务模式】之ICS(一):业务详述
PS:本专题系列讲述如何在SAP系统中实现ICS的业务模式,本系列博文系原创,如要转载引用,请保持原文一致并注明出处! SAP系统自身功能非常强大,支持多种业务模式,通过前台后台的配置就可以实现多种效 ...
- ionic第二坑——ionic 上拉菜单(ActionSheet)安卓样式坑
闲话不说,先上图: 这是IOS上的显示效果,代码如下: HTML部分: <body ng-app="starter" ng-controller="actionsh ...
- java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE
Android发出HTTP请求时出现了这个错误: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INST ...
- CentOS下Zabbix安装部署及汉化
搭建环境:Centos6.5_x86_64,Zabbix2.4.5,epel 源 服务端: 1.安装开发软件包yum -y groupinstall "Development Tools&q ...
- MemoryMappedFile 在 Mono in Linux 的开发笔记
前言 MemoryMappedFile(简称MMF)类是.NET中对内存映射文件进行操作的类,内存映射文件是非常高效的本地IO方案,由操作系统提供内存与IO文件之间的映射转换,对内存映射文件的更改由操 ...