Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
Signed-off-by: virtual <1185513330@qq.com>
  • Loading branch information
cocosip committed Dec 25, 2024
1 parent 3295f87 commit 36d203f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>6.3.0</Version>
<Version>6.3.1</Version>
<Authors>virtual</Authors>
<Product>DotCommon</Product>
<PackageProjectUrl>https://github.com/cocosip/DotCommon</PackageProjectUrl>
Expand Down
33 changes: 33 additions & 0 deletions src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DotCommon.Caching
{
/// <summary>ServiceCollection扩展方法
/// </summary>
public static class ServiceCollectionExtensions
{

/// <summary>添加分布式缓存
/// </summary>
public static IServiceCollection AddGenericsMemoryCache(this IServiceCollection services)
{
services.AddMemoryCache();
services.AddDistributedMemoryCache();
services.AddSingleton(typeof(IDistributedCache<>), typeof(DistributedCache<>));
return services;
}

/// <summary>添加分布式缓存
/// </summary>
public static IServiceCollection AddGenericsMemoryCache(this IServiceCollection services, Action<MemoryCacheOptions> memoryCacheOptions, Action<MemoryDistributedCacheOptions> memoryDistributedCacheOptions)
{
services.AddMemoryCache(memoryCacheOptions);
services.AddDistributedMemoryCache(memoryDistributedCacheOptions);
services.AddSingleton(typeof(IDistributedCache<>), typeof(DistributedCache<>));
return services;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Reflection;
using System.Threading.Tasks;

namespace DotCommon
namespace DotCommon.Threading
{
/// <summary>异步工具类
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions src/DotCommon.Caching/Threading/AsyncOneTimeRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Nito.AsyncEx;
using System;
using System.Threading.Tasks;

namespace DotCommon.Threading
{
/// <summary>异步一次运行方法
/// </summary>
public class AsyncOneTimeRunner
{
private volatile bool _runBefore;
private readonly AsyncLock _asyncLock = new AsyncLock();

/// <summary>Ctor
/// </summary>
public async Task RunAsync(Func<Task> action)
{
if (_runBefore)
{
return;
}

using (await _asyncLock.LockAsync())
{
if (_runBefore)
{
return;
}

await action();

_runBefore = true;
}
}
}
}
13 changes: 7 additions & 6 deletions src/DotCommon/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DotCommon.ObjectMapping;
using DotCommon.Scheduling;
using DotCommon.Serializing;
using DotCommon.Threading;
using Microsoft.Extensions.DependencyInjection;

namespace DotCommon.DependencyInjection
Expand All @@ -19,14 +20,14 @@ public static IServiceCollection AddDotCommon(this IServiceCollection services)
services
.AddTransient<IJsonSerializer, DefaultJsonSerializer>()
.AddTransient<IXmlSerializer, DefaultXmlSerializer>()
//.AddTransient<IBinarySerializer, DefaultBinarySerializer>()
//.AddTransient<IObjectSerializer, DefaultObjectSerializer>()
.AddTransient<IBinarySerializer, DefaultBinarySerializer>()
.AddTransient<IObjectSerializer, DefaultObjectSerializer>()
.AddSingleton<IScheduleService, ScheduleService>()
.AddSingleton<IObjectMapper, NullObjectMapper>()
////生命周期管理
//.AddSingleton<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance)
//.AddSingleton<IAmbientDataContext, AsyncLocalAmbientDataContext>()
//.AddSingleton(typeof(IAmbientScopeProvider<>), typeof(AmbientDataContextAmbientScopeProvider<>))
//生命周期管理
.AddSingleton<ICancellationTokenProvider>(NullCancellationTokenProvider.Instance)
.AddSingleton<IAmbientDataContext, AsyncLocalAmbientDataContext>()
.AddSingleton(typeof(IAmbientScopeProvider<>), typeof(AmbientDataContextAmbientScopeProvider<>))
;
return services;
}
Expand Down

0 comments on commit 36d203f

Please sign in to comment.