feat: 支持分布式锁

This commit is contained in:
橙子
2025-02-01 21:53:05 +08:00
parent d1c1eed52e
commit 5dea4ab18c
3 changed files with 62 additions and 5 deletions

View File

@@ -20,7 +20,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="Volo.Abp.DistributedLocking" Version="$(AbpVersion)" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" /> <ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" /> <ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />

View File

@@ -1,6 +1,10 @@
using Microsoft.Extensions.DependencyInjection; using Medallion.Threading;
using Medallion.Threading.Redis;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using Volo.Abp.AspNetCore.SignalR; using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.DistributedLocking;
using Volo.Abp.Domain; using Volo.Abp.Domain;
using Volo.Abp.Imaging; using Volo.Abp.Imaging;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@@ -20,7 +24,8 @@ namespace Yi.Framework.Rbac.Domain
typeof(AbpAspNetCoreSignalRModule), typeof(AbpAspNetCoreSignalRModule),
typeof(AbpDddDomainModule), typeof(AbpDddDomainModule),
typeof(AbpCachingModule), typeof(AbpCachingModule),
typeof(AbpImagingImageSharpModule) typeof(AbpImagingImageSharpModule),
typeof(AbpDistributedLockingModule)
)] )]
public class YiFrameworkRbacDomainModule : AbpModule public class YiFrameworkRbacDomainModule : AbpModule
{ {
@@ -36,6 +41,15 @@ namespace Yi.Framework.Rbac.Domain
//配置阿里云短信 //配置阿里云短信
Configure<AliyunOptions>(configuration.GetSection(nameof(AliyunOptions))); Configure<AliyunOptions>(configuration.GetSection(nameof(AliyunOptions)));
//分布式锁
context.Services.AddSingleton<IDistributedLockProvider>(sp =>
{
var connection = ConnectionMultiplexer
.Connect(configuration["Redis:Configuration"]);
return new
RedisDistributedSynchronizationProvider(connection.GetDatabase());
});
} }
} }
} }

View File

@@ -1,8 +1,10 @@
using System.Xml.Linq; using System.Xml.Linq;
using Mapster; using Mapster;
using Medallion.Threading;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.RateLimiting;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.DistributedLocking;
using Volo.Abp.Settings; using Volo.Abp.Settings;
using Volo.Abp.Uow; using Volo.Abp.Uow;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Banner; using Yi.Framework.Bbs.Application.Contracts.Dtos.Banner;
@@ -138,6 +140,7 @@ namespace Yi.Abp.Application.Services
} }
private static int RequestNumber { get; set; } = 0; private static int RequestNumber { get; set; } = 0;
/// <summary> /// <summary>
/// 速率限制 /// 速率限制
/// </summary> /// </summary>
@@ -154,6 +157,7 @@ namespace Yi.Abp.Application.Services
public ISettingProvider _settingProvider { get; set; } public ISettingProvider _settingProvider { get; set; }
public ISettingManager _settingManager { get; set; } public ISettingManager _settingManager { get; set; }
/// <summary> /// <summary>
/// 系统配置模块 /// 系统配置模块
/// </summary> /// </summary>
@@ -175,5 +179,41 @@ namespace Yi.Abp.Application.Services
return result ?? string.Empty; return result ?? string.Empty;
} }
/// <summary>
/// 分布式送abp版本abp套了一层娃。但是纯粹鸡肋不建议使用这个
/// </summary>
public IAbpDistributedLock AbpDistributedLock { get; set; }
/// <summary>
/// 分布式锁推荐使用版本yyds分布式锁永远的神
/// </summary>
public IDistributedLockProvider DistributedLock { get; set; }
/// <summary>
/// 分布式锁
/// </summary>
/// <remarks>强烈吐槽一下abp正如他们所说abp的分布式锁单纯为了自己用一切还是以DistributedLock为主</remarks>>
/// <returns></returns>
public async Task<string> GetDistributedLockAsync()
{
var number = 0;
await Parallel.ForAsync(0, 100, async (i, cancellationToken) =>
{
await using (await DistributedLock.AcquireLockAsync("MyLockName"))
{
//执行1秒
number += 1;
}
});
var number2 = 0;
await Parallel.ForAsync(0, 100, async (i, cancellationToken) =>
{
//执行1秒
number2 += 1;
});
return $"加锁结果:{number},不加锁结果:{number2}";
}
} }
} }