-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from yileicn/hotfix
Hotfix
- Loading branch information
Showing
11 changed files
with
131 additions
and
115 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
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,23 @@ | ||
<Project> | ||
<PropertyGroup Label="Common package properties"> | ||
<Authors>RabbitYi</Authors> | ||
<Copyright>Copyright (c) RabbitYi contributors. </Copyright> | ||
<PackageTags>grpc,dashboard,consul,micorservice,opentracing,polly</PackageTags> | ||
<PackageProjectUrl>https://github.com/yileicn/Grpc.Extensions/</PackageProjectUrl> | ||
<PackageIconUrl></PackageIconUrl> | ||
<RepositoryUrl>https://github.com/yileicn/Grpc.Extensions/</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageReleaseNotes></PackageReleaseNotes> | ||
<NoPackageAnalysis>true</NoPackageAnalysis> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild> | ||
<Version>1.4.9</Version> | ||
<Description>一个基于GRPC的简单微服务框架 | ||
1.服务注册和发现 | ||
2.服务自动负载均衡 | ||
3.服务端中件间(性能监控[日志],全局错误处理,手动熔断) | ||
4.客户端中件间(认证,超时时间设置) | ||
5.DashBoard(远程调用,手动熔断,日志输出控制) | ||
6.Grpc CodeFirst</Description> | ||
</PropertyGroup> | ||
</Project> |
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
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
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,52 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Grpc.Extension.Common | ||
{ | ||
/// <summary> | ||
/// MemoryCacheExtensions(Thread Safe) | ||
/// </summary> | ||
public static class MemoryCacheExtensions | ||
{ | ||
private static readonly LazyConcurrentDictionary<int, SemaphoreSlim> _semaphores = new LazyConcurrentDictionary<int, SemaphoreSlim>(); | ||
|
||
public static async Task<T> GetOrCreateAtomicAsync<T>(this IMemoryCache memoryCache, object key, Func<ICacheEntry, Task<T>> factory) | ||
{ | ||
if (memoryCache.TryGetValue(key, out var value)) | ||
return (T)value; | ||
|
||
var semaphoreKey = (memoryCache, key).GetHashCode(); | ||
var semaphore = GetSemaphore(semaphoreKey); | ||
|
||
await semaphore.WaitAsync() | ||
.ConfigureAwait(false); | ||
try | ||
{ | ||
if (!memoryCache.TryGetValue(key, out value)) | ||
{ | ||
var entry = memoryCache.CreateEntry(key); | ||
value = await factory(entry); | ||
entry.SetValue(value); | ||
entry.Dispose(); | ||
return (T)value; | ||
} | ||
|
||
return (T)value; | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
|
||
private static SemaphoreSlim GetSemaphore(int semaphoreKey) | ||
{ | ||
return _semaphores.GetOrAdd(semaphoreKey, k => new SemaphoreSlim(1)); | ||
} | ||
} | ||
} |
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
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