参考自:http://redis.io/topics/transactions 官网
redis transactions 事务管理作用
多条指令排队,接收到执行指令,才开始执行,redis 2.6.5版本以后是多条指令全部执行成功,才算成功,否则回复到先前状态
redis transactions 相关命令
MULTI:进入transactions,返回OK,redis会对进入transactions的指令(commands)排队,一旦执行EXEC 命令 所有进入transactions 的指令(commands)开始执行
EXEC:一旦执行EXEC 命令 所有进入transactions 的commands开始执行,成功返回一个响应数组,失败返回nil(null)
DISCARD: 中断一个transaction,不执行MULTI里面的多条指令
WATCH:( check-and-set (CAS) behavior)监控某个key,操作变量,如果同时有多个 watched key 在执行EXEC前正在被修改,则transaction停止,
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
加入一条指令,进入排队,返回queued,遇到EXEC指令开始执行MULT内的指令,返回一个响应数组
WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
如果在watchh 和exec之间有多个mykey正在被修改,则transaction停止
java 客户端 Jedis实现
1.new Jedis(?,?)创建连接
2.jedis.watch("key"); 开始监听,一旦有两个及以上对该key操作则事务停止
3..Transaction transaction = jedis.multi();//multi开启事务
4.指令入队列
5.exec执行事务
package com.danny.jedisUtil;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
public class TransactionsTest {
private static Log logger = LogFactory.getLog(TransactionsTest.class.getName());
private static void withoutTransactions(int count){
Jedis jedis = null;
try{
jedis = new Jedis("XXX",6380);//redis 服务器IP
for (int i = 0; i < count; i++) {
String result = jedis.set("n" + i, "n" + i);
}
}catch(Exception e){
logger.error(e);
}finally{
if(jedis != null){
jedis.disconnect();
}
}
}
private static void useTransactions(int count){
Jedis jedis = null;
try{
jedis = new Jedis("XXX",6380);//redis 服务器IP,端口
jedis.watch("foo");
//multi,开启事务
Transaction transaction = jedis.multi();
for (int i = 0; i < count; i++) {
//指令一依次放入队列,每一条指令成功放入队列,Response<String> exec指令执行前不执行该指令
transaction.set("n" + i, "n" + i);
}
//transaction.set("foo", "bar");
//Response<String> result1 = transaction.get("foo"); //redis String类型
// transaction.zadd("foo", 1, "barowitch");
// transaction.zadd("foo", 0, "barinsky");
// transaction.zadd("foo", 0, "carty");
// Response<Set<String>> sose = transaction.zrange("foo", 0, -1);//redis set<String>类型
//exec指令,开始执行队列里的指令,成功返回一个数值数组,否则返回null
List<Object> results = transaction.exec();
// String foolbar = result1.get(); // use Response.get() to retrieve things from a Response
// int soseSize = sose.get().size(); // on sose.get() you can directly call Set methods!
// System.out.println("result1:" + foolbar);
// System.out.println("soseSize:" + soseSize);
}catch(Exception e){
logger.error(e);
}finally{
if(jedis != null){
jedis.disconnect();
}
}
}
public static void main(String[] args){
int count = 10;
long start = System.currentTimeMillis();
withoutTransactions(count);
long end = System.currentTimeMillis();
System.out.println("withoutTransaction: " + (end-start) + "ms");
start = System.currentTimeMillis();
useTransactions(count);
end = System.currentTimeMillis();
System.out.println("useTransaction: " + (end-start) + "ms");
}
}