コード例 #1
0
        /// <summary>
        /// 使用事务。
        /// </summary>
        /// <param name="action"></param>
        public void UseTransaction(string token, Action action, TimeSpan timeout)
        {
            if (RedisTransactionContext.Current != null)
            {
                action();
                return;
            }

            using var scope = new RedisTransactionContext();
            token           = ServiceProvider.GetCacheKey(token);
            RedisHelper.Lock(GetConnection(token), token, timeout, action);
        }
コード例 #2
0
        /// <summary>
        /// 异步的,使用事务。
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public async Task UseTransactionAsync(string token, Func <Task> func, TimeSpan timeout, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (RedisTransactionContext.Current != null)
            {
                await func();

                return;
            }

            using var scope = new RedisTransactionContext();
            token           = ServiceProvider.GetCacheKey(token);
            await RedisHelper.LockAsync(GetConnection(token), token, timeout, func);
        }