分布式锁 sqlserver 测试 03
parent
7544e0c2e9
commit
1afe020e83
|
@ -1,8 +1,12 @@
|
|||
using IRaCIS.Core.Application.Triggers;
|
||||
using Hangfire.SqlServer;
|
||||
using IRaCIS.Core.Application.Triggers;
|
||||
using IRaCIS.Core.Infra.EFCore;
|
||||
using Medallion.Threading;
|
||||
using Medallion.Threading.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace IRaCIS.Core.API
|
||||
{
|
||||
|
@ -52,6 +56,13 @@ namespace IRaCIS.Core.API
|
|||
|
||||
});
|
||||
|
||||
//注意区分 easy caching 也有 IDistributedLockProvider
|
||||
services.AddSingleton<IDistributedLockProvider>(sp =>
|
||||
{
|
||||
//var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]!);
|
||||
|
||||
return new SqlDistributedSynchronizationProvider(configuration.GetSection("ConnectionStrings:RemoteNew").Value);
|
||||
});
|
||||
|
||||
//services.AddAssemblyTriggers(typeof(SubjectVisitImageDateTrigger).Assembly);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
using IRaCIS.Core.Application.Service;
|
||||
using DocumentFormat.OpenXml.Office2010.Excel;
|
||||
using IRaCIS.Core.Application.Service;
|
||||
using IRaCIS.Core.Application.ViewModel;
|
||||
using IRaCIS.Core.Domain.Share;
|
||||
using IRaCIS.Core.Infrastructure;
|
||||
using MassTransit;
|
||||
using Medallion.Threading;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -10,6 +12,7 @@ using Microsoft.Extensions.Caching.Distributed;
|
|||
using Microsoft.Extensions.Options;
|
||||
using MiniExcelLibs;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
@ -20,6 +23,8 @@ namespace IRaCIS.Application.Services
|
|||
[ApiExplorerSettings(GroupName = "Institution")]
|
||||
public class TestService : BaseService
|
||||
{
|
||||
|
||||
public static int IntValue = 100;
|
||||
private readonly IRepository<Dictionary> _dicRepository;
|
||||
private readonly IRepository<Trial> _trialRepository;
|
||||
|
||||
|
@ -30,10 +35,11 @@ namespace IRaCIS.Application.Services
|
|||
private readonly IOptionsMonitor<ServiceVerifyConfigOption> _basicConfig;
|
||||
private readonly IRepository<VisitTask> _visitTaskRepositoryy;
|
||||
|
||||
private readonly IDistributedLockProvider _distributedLockProvider;
|
||||
|
||||
public TestService(IRepository<Dictionary> dicRepository, IRepository<Trial> trialRepository/*, IDistributedCache cache*/
|
||||
|
||||
, IOptionsMonitor<SystemEmailSendConfig> systemEmailConfig, IOptionsMonitor<ServiceVerifyConfigOption> basicConfig, IRepository<VisitTask> visitTaskRepository)
|
||||
, IOptionsMonitor<SystemEmailSendConfig> systemEmailConfig, IOptionsMonitor<ServiceVerifyConfigOption> basicConfig, IRepository<VisitTask> visitTaskRepository, IDistributedLockProvider distributedLockProvider)
|
||||
{
|
||||
_visitTaskRepositoryy = visitTaskRepository;
|
||||
|
||||
|
@ -42,10 +48,31 @@ namespace IRaCIS.Application.Services
|
|||
|
||||
_dicRepository = dicRepository;
|
||||
_trialRepository = trialRepository;
|
||||
|
||||
_distributedLockProvider= distributedLockProvider;
|
||||
//_cache = cache;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[AllowAnonymous]
|
||||
public async Task<IResponseOutput> TestDistributedLock( )
|
||||
{
|
||||
Console.WriteLine($"我进来了当前值是:" + IntValue);
|
||||
|
||||
var @lock = _distributedLockProvider.CreateLock($"UserAccount");
|
||||
|
||||
using (await @lock.AcquireAsync())
|
||||
{
|
||||
await Task.Delay(3);
|
||||
IntValue--;
|
||||
Console.WriteLine(IntValue);
|
||||
}
|
||||
|
||||
return ResponseOutput.Ok(IntValue);
|
||||
}
|
||||
|
||||
|
||||
[AllowAnonymous]
|
||||
public async Task<IResponseOutput> GetMemoryStoreData()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue