重构代码并抽取基础构建库

This commit is contained in:
administrator committed 2025-03-20 22:13:45 +08:00
1 parent 9e6b5be92c
commit ccf1e7672d
55 files changed
+398 -104

No files matched your search

@@ -0,0 +1,25 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Dapr.Client;
using System.Diagnostics;
namespace HelloShop.DistributedLock.Dapr
{
public class DaprDistributedLock(DaprClient daprClient) : IDistributedLock
{
public async Task<IDistributedLockResult> LockAsync(string resourceId, int expiryInSeconds = default, CancellationToken cancellationToken = default)
{
expiryInSeconds = expiryInSeconds == default ? 60 : expiryInSeconds;
// The CallerMemberNameAttribute is used to get the name of the calling method.
string? lockOwner = new StackTrace().GetFrame(1)?.GetMethod()?.DeclaringType?.Name;
#pragma warning disable CS0618
TryLockResponse response = await daprClient.Lock("lockstore", resourceId, lockOwner, expiryInSeconds, cancellationToken);
#pragma warning restore CS0618
return new DaprDistributedLockResult(response);
}
}
}
@@ -0,0 +1,17 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Microsoft.Extensions.DependencyInjection;
namespace HelloShop.DistributedLock.Dapr
{
public static class DaprDistributedLockExtensions
{
public static IServiceCollection AddDaprDistributedLock(this IServiceCollection services)
{
services.AddSingleton<IDistributedLock, DaprDistributedLock>();
return services;
}
}
}
@@ -0,0 +1,18 @@
// Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information.
using Dapr.Client;
namespace HelloShop.DistributedLock.Dapr
{
#pragma warning disable CS0618
public class DaprDistributedLockResult(TryLockResponse tryLockResponse) : IDistributedLockResult
#pragma warning restore CS0618
{
public async ValueTask DisposeAsync()
{
await tryLockResponse.DisposeAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
}
}
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HelloShop.DistributedLock\HelloShop.DistributedLock.csproj" />
</ItemGroup>
</Project>