本文整理汇总了C#中IDatabase.CreateTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# IDatabase.CreateTransaction方法的具体用法?C# IDatabase.CreateTransaction怎么用?C# IDatabase.CreateTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDatabase
的用法示例。
在下文中一共展示了IDatabase.CreateTransaction方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetUp
public void SetUp()
{
SetupEvent.WaitOne();
database = CreateDatabase();
if (storeType == StoreType.FileSystem)
((FileSystemDatabase) database).Start();
else {
((MemoryDatabase)database).Start();
}
transaction = database.CreateTransaction();
}
示例2: ManualIncr
public async Task<long?> ManualIncr(IDatabase connection, string key)
{
var oldVal = (long?)await connection.StringGetAsync(key);
var newVal = (oldVal ?? 0) + 1;
var tran = connection.CreateTransaction();
{ // check hasn't changed
#pragma warning disable 4014
tran.AddCondition(Condition.StringEqual(key, oldVal));
tran.StringSetAsync(key, newVal);
#pragma warning restore 4014
if (!await tran.ExecuteAsync()) return null; // aborted
return newVal;
}
}
示例3: LockedAction
private static bool LockedAction(IDatabase db, string sessionID, string lockId, Action<ITransaction> action)
{
RedisKey keyLock = Tools.MakeLockKey(sessionID);
Debug.Assert(!String.IsNullOrEmpty(lockId));
var trans = db.CreateTransaction();
trans.AddCondition(Condition.StringEqual(keyLock, lockId));
action(trans);
if (trans.Execute())
return true;
// failed
var delta = (DateTime.Now - lockStart).TotalMilliseconds;
Console.WriteLine("Lock {0} on key {1} held for {2} msec expired unexpectedly", lockId, sessionID, delta);
return false;
}
示例4: RemoveServer
public static void RemoveServer(IDatabase redis, string serverId)
{
var transaction = redis.CreateTransaction();
transaction.SetRemoveAsync(
RedisStorage.Prefix + "servers",
serverId);
transaction.KeyDeleteAsync(
new RedisKey[]
{
string.Format(RedisStorage.Prefix + "server:{0}", serverId),
string.Format(RedisStorage.Prefix + "server:{0}:queues", serverId)
});
transaction.Execute();
}
示例5: SyncGeneration
private void SyncGeneration(IDatabase db)
{
var generationKey = CacheNamespace.GetGenerationKey();
var serverGenerationValue = db.StringGet(generationKey);
var serverGeneration = Convert.ToInt64(serverGenerationValue);
var currentGeneration = CacheNamespace.GetGeneration();
// Generation was cleared by someone else (shouldn't happen).
if (serverGenerationValue.IsNullOrEmpty)
{
db.StringSetAsync(
key: generationKey,
value: currentGeneration,
// Only set if someone else doesn't jump in and set it first.
when: When.NotExists,
flags: CommandFlags.FireAndForget
);
log.InfoFormat("setting server generation ({0}) because it is empty", currentGeneration);
}
// Generation was lowered by someone else (shouldn't happen).
else if (serverGeneration < CacheNamespace.GetGeneration())
{
var transaction = db.CreateTransaction();
// Only set if someone else doesn't jump in and set it first.
transaction.AddCondition(Condition.StringEqual(generationKey, serverGeneration));
transaction.StringSetAsync(
key: generationKey,
value: CacheNamespace.GetGeneration(),
flags: CommandFlags.FireAndForget
);
// We don't need to worry about the result because we will
// already retry if we can't sync the generation.
transaction.ExecuteAsync(CommandFlags.FireAndForget);
log.InfoFormat("syncing server generation (server={0}, current={1})", serverGeneration, currentGeneration);
}
else
{
CacheNamespace.SetHigherGeneration(serverGeneration);
log.InfoFormat("syncing server generation (server={0}, current={1})", serverGeneration, currentGeneration);
}
}
示例6: Commit
private static void Commit(IDatabase redis, Action<RedisWriteOnlyTransaction> action)
{
using (var transaction = new RedisWriteOnlyTransaction(redis.CreateTransaction()))
{
action(transaction);
transaction.Commit();
}
}
示例7: Commit
private void Commit(IDatabase connection, Action<RedisWriteOnlyTransaction> action)
{
using (var transaction = new RedisWriteOnlyTransaction(connection.CreateTransaction(), Redis.Storage.GetSubscribe(), Prefix))
{
action(transaction);
transaction.Commit();
}
}
示例8: LockSession
private static bool LockSession(IDatabase db, string sessionID, uint msec, out string lockId)
{
RedisKey keyLock = Tools.MakeLockKey(sessionID);
lockId = Guid.NewGuid().ToString();
var trans = db.CreateTransaction();
trans.AddCondition(Condition.KeyNotExists(keyLock));
trans.StringSetAsync(keyLock, lockId, TimeSpan.FromMilliseconds(msec));
if( trans.Execute())
{
lockStart = DateTime.Now;
//Console.WriteLine(" Aquired {0} on key {1}", lockId, keyLock);
return true;
}
lockId = String.Empty;
return false;
}
示例9: CreateTransaction
private SystemTransaction CreateTransaction(IDatabase database, User user)
{
if (database == null)
throw new InvalidOperationException("No database selected");
// The commit version number of this transaction
long transactionVersionNumber;
lock (commitSyncRoot) {
transactionVersionNumber = currentCommitVersion;
}
return new SystemTransaction(this, database.CreateTransaction(), transactionVersionNumber, user);
}
示例10: Save
public override bool Save(object obj, Type objType, IDatabase redisDatabase, string id, PropertyInfo basePropertyInfo = null)
{
var listKey = new RedisKeyObject(basePropertyInfo, id);
var trans = redisDatabase.CreateTransaction();
foreach (var o in ((IEnumerable<RedisValue>) obj).ToArray())
{
trans.ListRemoveAsync(listKey.RedisKey, o);
trans.ListLeftPushAsync(listKey.RedisKey, o);
}
trans.Execute();
return true;
}
示例11: CreateAdminSession
protected virtual ISession CreateAdminSession(IDatabase database)
{
var user = database.Authenticate(AdminUserName, AdminPassword);
var transaction = database.CreateTransaction(IsolationLevel.Serializable);
return new Session(transaction, user);
}
示例12: RemoveServer
public void RemoveServer(IDatabase redis, string serverId)
{
var transaction = redis.CreateTransaction();
transaction.SetRemoveAsync(Prefix + "servers", serverId);
transaction.KeyDeleteAsync(String.Format(Prefix + "server:{0}", serverId));
transaction.KeyDeleteAsync(String.Format(Prefix + "server:{0}:queues", serverId));
transaction.Execute();
}