Add db.AsyncLock

This commit is contained in:
sunkaixuan
2023-04-15 08:33:02 +08:00
parent 4e817696cf
commit 7d3117a6c0
6 changed files with 50 additions and 1 deletions

View File

@@ -1632,6 +1632,11 @@ namespace SqlSugar
#endregion #endregion
#region Other #region Other
public Task<SugarAsyncLock> AsyncLock(int timeOutSeconds = 30)
{
var result = new SugarAsyncLock(this);
return result.AsyncLock(timeOutSeconds);
}
public DynamicBuilder DynamicBuilder() public DynamicBuilder DynamicBuilder()
{ {
return new DynamicBuilder(this.Context); return new DynamicBuilder(this.Context);

View File

@@ -114,7 +114,10 @@ namespace SqlSugar
} }
#region API #region API
public Task<SugarAsyncLock> AsyncLock(int timeOutSeconds = 30)
{
return ScopedContext.AsyncLock(timeOutSeconds);
}
public SugarActionType SugarActionType { get => ScopedContext.SugarActionType; set => ScopedContext.SugarActionType = value; } public SugarActionType SugarActionType { get => ScopedContext.SugarActionType; set => ScopedContext.SugarActionType = value; }
public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; } public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; }
public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns = value; } public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns = value; }

View File

@@ -46,6 +46,7 @@ namespace SqlSugar
#endregion #endregion
#region Other methods #region Other methods
Task<SugarAsyncLock> AsyncLock(int timeOutSeconds = 30);
DynamicBuilder DynamicBuilder(); DynamicBuilder DynamicBuilder();
void Tracking<T>(T data) where T : class, new(); void Tracking<T>(T data) where T : class, new();
void Tracking<T>(List<T> data) where T : class, new(); void Tracking<T>(List<T> data) where T : class, new();

View File

@@ -1189,6 +1189,10 @@ namespace SqlSugar
#endregion #endregion
#region Helper #region Helper
public Task<SugarAsyncLock> AsyncLock(int timeOutSeconds = 30)
{
return this.Context.AsyncLock(timeOutSeconds);
}
public SplitTableContext SplitHelper<T>() where T:class,new() public SplitTableContext SplitHelper<T>() where T:class,new()
{ {
return this.Context.SplitHelper<T>(); return this.Context.SplitHelper<T>();

View File

@@ -851,5 +851,9 @@ namespace SqlSugar
{ {
ScopedContext.RemoveConnection(configId); ScopedContext.RemoveConnection(configId);
} }
public Task<SugarAsyncLock> AsyncLock(int timeOutSeconds=30)
{
return ScopedContext.AsyncLock(timeOutSeconds);
}
} }
} }

View File

@@ -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<SugarAsyncLock> AsyncLock(int timeOutSeconds)
{
TimeSpan timeout = TimeSpan.FromSeconds(timeOutSeconds);
await SemaphoreSlim.WaitAsync(timeout);
return this;
}
public void Dispose()
{
SemaphoreSlim.Release();
}
}
}