From 36d203f5f4e5625befd288c16c7829209af601f9 Mon Sep 17 00:00:00 2001 From: virtual <1185513330@qq.com> Date: Wed, 25 Dec 2024 14:20:42 +0800 Subject: [PATCH] fix bug Signed-off-by: virtual <1185513330@qq.com> --- common.props | 2 +- .../Caching/ServiceCollectionExtensions.cs | 33 +++++++++++++++++ .../{ => Threading}/AsyncHelper.cs | 2 +- .../Threading/AsyncOneTimeRunner.cs | 36 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 13 +++---- 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs rename src/DotCommon.Caching/{ => Threading}/AsyncHelper.cs (98%) create mode 100644 src/DotCommon.Caching/Threading/AsyncOneTimeRunner.cs diff --git a/common.props b/common.props index e9fa52f..4760129 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 6.3.0 + 6.3.1 virtual DotCommon https://github.com/cocosip/DotCommon diff --git a/src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs b/src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..ded9c12 --- /dev/null +++ b/src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace DotCommon.Caching +{ + /// ServiceCollection扩展方法 + /// + public static class ServiceCollectionExtensions + { + + /// 添加分布式缓存 + /// + public static IServiceCollection AddGenericsMemoryCache(this IServiceCollection services) + { + services.AddMemoryCache(); + services.AddDistributedMemoryCache(); + services.AddSingleton(typeof(IDistributedCache<>), typeof(DistributedCache<>)); + return services; + } + + /// 添加分布式缓存 + /// + public static IServiceCollection AddGenericsMemoryCache(this IServiceCollection services, Action memoryCacheOptions, Action memoryDistributedCacheOptions) + { + services.AddMemoryCache(memoryCacheOptions); + services.AddDistributedMemoryCache(memoryDistributedCacheOptions); + services.AddSingleton(typeof(IDistributedCache<>), typeof(DistributedCache<>)); + return services; + } + + } +} diff --git a/src/DotCommon.Caching/AsyncHelper.cs b/src/DotCommon.Caching/Threading/AsyncHelper.cs similarity index 98% rename from src/DotCommon.Caching/AsyncHelper.cs rename to src/DotCommon.Caching/Threading/AsyncHelper.cs index d21195b..d06fbc4 100644 --- a/src/DotCommon.Caching/AsyncHelper.cs +++ b/src/DotCommon.Caching/Threading/AsyncHelper.cs @@ -3,7 +3,7 @@ using System.Reflection; using System.Threading.Tasks; -namespace DotCommon +namespace DotCommon.Threading { /// 异步工具类 /// diff --git a/src/DotCommon.Caching/Threading/AsyncOneTimeRunner.cs b/src/DotCommon.Caching/Threading/AsyncOneTimeRunner.cs new file mode 100644 index 0000000..fcddee4 --- /dev/null +++ b/src/DotCommon.Caching/Threading/AsyncOneTimeRunner.cs @@ -0,0 +1,36 @@ +using Nito.AsyncEx; +using System; +using System.Threading.Tasks; + +namespace DotCommon.Threading +{ + /// 异步一次运行方法 + /// + public class AsyncOneTimeRunner + { + private volatile bool _runBefore; + private readonly AsyncLock _asyncLock = new AsyncLock(); + + /// Ctor + /// + public async Task RunAsync(Func action) + { + if (_runBefore) + { + return; + } + + using (await _asyncLock.LockAsync()) + { + if (_runBefore) + { + return; + } + + await action(); + + _runBefore = true; + } + } + } +} diff --git a/src/DotCommon/DependencyInjection/ServiceCollectionExtensions.cs b/src/DotCommon/DependencyInjection/ServiceCollectionExtensions.cs index 6e15f63..5181854 100644 --- a/src/DotCommon/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/DotCommon/DependencyInjection/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ using DotCommon.ObjectMapping; using DotCommon.Scheduling; using DotCommon.Serializing; +using DotCommon.Threading; using Microsoft.Extensions.DependencyInjection; namespace DotCommon.DependencyInjection @@ -19,14 +20,14 @@ public static IServiceCollection AddDotCommon(this IServiceCollection services) services .AddTransient() .AddTransient() - //.AddTransient() - //.AddTransient() + .AddTransient() + .AddTransient() .AddSingleton() .AddSingleton() - ////生命周期管理 - //.AddSingleton(NullCancellationTokenProvider.Instance) - //.AddSingleton() - //.AddSingleton(typeof(IAmbientScopeProvider<>), typeof(AmbientDataContextAmbientScopeProvider<>)) + //生命周期管理 + .AddSingleton(NullCancellationTokenProvider.Instance) + .AddSingleton() + .AddSingleton(typeof(IAmbientScopeProvider<>), typeof(AmbientDataContextAmbientScopeProvider<>)) ; return services; }