本文整理汇总了C#中BookSleeve.RedisConnection.CreateTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# RedisConnection.CreateTransaction方法的具体用法?C# RedisConnection.CreateTransaction怎么用?C# RedisConnection.CreateTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BookSleeve.RedisConnection
的用法示例。
在下文中一共展示了RedisConnection.CreateTransaction方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ManualIncr
public async Task<long?> ManualIncr(RedisConnection connection, int db, string key)
{
var oldVal = await connection.Strings.GetInt64(db, key);
var newVal = (oldVal ?? 0) + 1;
using (var tran = connection.CreateTransaction())
{ // check hasn't changed
#pragma warning disable 4014
tran.AddCondition(Condition.KeyEquals(db, key, oldVal));
tran.Strings.Set(db, key, newVal);
#pragma warning restore 4014
if (!await tran.Execute()) return null; // aborted
return newVal;
}
}
示例2: TestMethod1
public void TestMethod1()
{
using (RedisConnection connection = new RedisConnection("localhost"))
{
connection.Open();
RedisTransaction transaction = connection.CreateTransaction();
string key = DateTime.Now.ToString();
transaction.Lists.AddFirst(1, key, "pankaj", createIfMissing: true);
transaction.Execute();
connection.Keys.Exists(1, key);
}
}
示例3: Send
static async Task<int> Send(RedisConnection conn, string idKey, int db, string channel, string data)
{
int attempts = 0;
bool success;
do
{
var oldId = await conn.Strings.GetInt64(db, idKey).SafeAwaitable().ConfigureAwait(false); // important: let this be nullable;
// means "doesn't exist"
var newId = (oldId ?? 0) + 1;
var payload = Pack(newId, data);
using (var tran = conn.CreateTransaction())
{
var x0 = tran.AddCondition(Condition.KeyEquals(db, idKey, oldId)).SafeAwaitable();
var x1 = tran.Strings.Increment(db, idKey).SafeAwaitable();
var x2 = tran.Publish(channel, payload).SafeAwaitable();
success = await tran.Execute().SafeAwaitable().ConfigureAwait(false);
if (success)
{
// still expect all of these to get answers
await Task.WhenAll(x0, x1, x2);
Assert.IsTrue(x0.Result, "condition passed");
Assert.AreEqual(newId, x1.Result);
}
else
{
// can't say much about x0; could have got past that
Assert.IsTrue(await IsCancelled(x1));
Assert.IsTrue(await IsCancelled(x2));
}
attempts++;
}
} while (!success);
return attempts;
}