-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: virtual <1185513330@qq.com>
- Loading branch information
Showing
5 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/DotCommon.Caching/Caching/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters