redis研究之watch
具体的看注释,代码如下:
1 public static void TransTest() 2 { 3 RedisClient client1 = new RedisClient(host, port); 4 RedisClient client2 = new RedisClient(host, port); 5 //初始k1为1 6 client1.Set<string>("k1", "1"); 7 Console.WriteLine(client1.Get<string>("k1")); 8 //监视k1 9 client1.Watch("k1"); 10 var trans = client1.CreateTransaction(); 11 trans.QueueCommand(r => r.Set<string>("k1", "11111111111111")); 12 //k2不存在 13 trans.QueueCommand(r => r.Set<string>("k2", "2")); 14 //其它客户端在事务提交前修改k1的值 15 client2.Set<string>("k1", "AAAAAAAAAA"); 16 //提交事务 17 trans.Commit(); 18 Console.WriteLine(client1.Get<string>("k1"));//这里会输出AAAAAAAAAA 19 Console.WriteLine(client1.Get<string>("k2"));//这个值会是空 20 Console.Read(); 21 }