From 7d3117a6c0a1c2816ff2bbfb768b0977129ad275 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 15 Apr 2023 08:33:02 +0800 Subject: [PATCH] Add db.AsyncLock --- .../SugarProvider/SqlSugarProvider.cs | 5 +++ .../SugarProvider/SqlSugarScopeProvider.cs | 5 ++- .../SqlSugar/Interface/ISqlSugarClient.cs | 1 + Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs | 4 +++ Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs | 4 +++ .../SqlSugar/Utilities/SugarAsyncLock.cs | 32 +++++++++++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Src/Asp.NetCore2/SqlSugar/Utilities/SugarAsyncLock.cs diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index a1a9d8ba5..299761810 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -1632,6 +1632,11 @@ namespace SqlSugar #endregion #region Other + public Task AsyncLock(int timeOutSeconds = 30) + { + var result = new SugarAsyncLock(this); + return result.AsyncLock(timeOutSeconds); + } public DynamicBuilder DynamicBuilder() { return new DynamicBuilder(this.Context); diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs index 4d86f8228..b4e42a5d0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs @@ -114,7 +114,10 @@ namespace SqlSugar } #region API - + public Task AsyncLock(int timeOutSeconds = 30) + { + return ScopedContext.AsyncLock(timeOutSeconds); + } public SugarActionType SugarActionType { get => ScopedContext.SugarActionType; set => ScopedContext.SugarActionType = value; } public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; } public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns = value; } diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs index 62d5d722d..1727e37c0 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs @@ -46,6 +46,7 @@ namespace SqlSugar #endregion #region Other methods + Task AsyncLock(int timeOutSeconds = 30); DynamicBuilder DynamicBuilder(); void Tracking(T data) where T : class, new(); void Tracking(List data) where T : class, new(); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index 4ffffd753..f81964e88 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -1189,6 +1189,10 @@ namespace SqlSugar #endregion #region Helper + public Task AsyncLock(int timeOutSeconds = 30) + { + return this.Context.AsyncLock(timeOutSeconds); + } public SplitTableContext SplitHelper() where T:class,new() { return this.Context.SplitHelper(); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs index 2ee4e1460..de9712f2e 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs @@ -851,5 +851,9 @@ namespace SqlSugar { ScopedContext.RemoveConnection(configId); } + public Task AsyncLock(int timeOutSeconds=30) + { + return ScopedContext.AsyncLock(timeOutSeconds); + } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Utilities/SugarAsyncLock.cs b/Src/Asp.NetCore2/SqlSugar/Utilities/SugarAsyncLock.cs new file mode 100644 index 000000000..f3a0e26bd --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Utilities/SugarAsyncLock.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SugarAsyncLock : IDisposable + { + static readonly SemaphoreSlim SemaphoreSlim =new SemaphoreSlim(1); + + + public SugarAsyncLock(SqlSugarProvider db) + { + + } + + public async Task AsyncLock(int timeOutSeconds) + { + TimeSpan timeout = TimeSpan.FromSeconds(timeOutSeconds); + await SemaphoreSlim.WaitAsync(timeout); + return this; + } + + public void Dispose() + { + SemaphoreSlim.Release(); + } + } +}