Synchronization code

This commit is contained in:
sunkaixuan
2023-04-15 11:22:44 +08:00
parent 7d3117a6c0
commit 440d099102
7 changed files with 51 additions and 1 deletions

View File

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

View File

@@ -114,7 +114,10 @@ namespace SqlSugar
}
#region API
public Task<SugarAsyncLock> 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; }

View File

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

View File

@@ -436,6 +436,7 @@
<Compile Include="Utilities\ExpressionBuilderHelper.cs" />
<Compile Include="Utilities\CommonExtensions.cs" />
<Compile Include="Utilities\PropertyCallAdapterProvider.cs" />
<Compile Include="Utilities\SugarAsyncLock.cs" />
<Compile Include="Utilities\SugarRetry.cs" />
<Compile Include="Utilities\DataTableExtensions.cs" />
<Compile Include="Utilities\ReflectionExtensions.cs" />

View File

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

View File

@@ -851,5 +851,9 @@ namespace SqlSugar
{
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();
}
}
}