You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace RedisStuy
{
/// <summary>
/// 字符串操作学习
/// http://www.redis.cn
/// http://www.redis.net.cn/order/
/// </summary>
public class RedisTransactionStudy
{
#region 初始化
private IDatabase redisDatabase;
public RedisTransactionStudy()
{
redisDatabase = RedisHelper.GetRedisDatabase();
}
#endregion
/// <summary>
/// 允许创建一组操作,这些操作将作为单个单元发送给服务器,但可能会或可能不会在服务器上连续处理
/// </summary>
public void Batch()
{
IBatch batch= redisDatabase.CreateBatch(null);
batch.KeyExistsAsync("sdfasfasdfasdf");
batch.KeyDeleteAsync("sdfasdfsfdafasdfasdfasdfasdf");
batch.Execute();
}
public void TestPipeLine()
{
var batch = redisDatabase.CreateBatch();
Task t1 = batch.StringSetAsync("name", "bob");
Task t2 = batch.StringSetAsync("age", 100);
batch.Execute();
Task.WaitAll(t1, t2);
}
public void TestTran()
{
IDatabase db = redisDatabase;
string name = db.StringGet("name");
string age = db.StringGet("age");
var tran = db.CreateTransaction();
tran.AddCondition(Condition.StringEqual("name", name));
Console.WriteLine("tran begin");
tran.StringSetAsync("name", "leap");
tran.StringSetAsync("age", 12);
Thread.Sleep(4000);
bool result = tran.Execute();
}
}
}