缓存枷锁

This commit is contained in:
陈国伟 2021-09-26 17:28:34 +08:00
parent 9516a22c26
commit a0cb04cec3

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
@ -20,9 +21,10 @@ namespace FastGithub.DomainResolve
{ {
private readonly ILogger<DnsClient> logger; private readonly ILogger<DnsClient> logger;
private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(2d).TotalMilliseconds; private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new();
private readonly IMemoryCache dnsCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly IMemoryCache dnsCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly TimeSpan dnsExpiration = TimeSpan.FromMinutes(2d); private readonly TimeSpan dnsExpiration = TimeSpan.FromMinutes(2d);
private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(2d).TotalMilliseconds;
/// <summary> /// <summary>
/// DNS客户端 /// DNS客户端
@ -43,18 +45,20 @@ namespace FastGithub.DomainResolve
public async Task<IPAddress[]> LookupAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default) public async Task<IPAddress[]> LookupAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default)
{ {
var key = $"{dns}:{domain}"; var key = $"{dns}:{domain}";
if (this.dnsCache.TryGetValue<IPAddress[]>(key, out var value)) var semaphore = this.semaphoreSlims.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
{
return value;
}
try try
{
await semaphore.WaitAsync(CancellationToken.None);
if (this.dnsCache.TryGetValue<IPAddress[]>(key, out var value) == false)
{ {
value = await this.LookupCoreAsync(dns, domain, cancellationToken); value = await this.LookupCoreAsync(dns, domain, cancellationToken);
this.dnsCache.Set(key, value, this.dnsExpiration); this.dnsCache.Set(key, value, this.dnsExpiration);
var items = string.Join(", ", value.Select(item => item.ToString())); var items = string.Join(", ", value.Select(item => item.ToString()));
this.logger.LogInformation($"{dns}{domain}->[{items}]"); this.logger.LogInformation($"{dns}{domain}->[{items}]");
}
return value; return value;
} }
catch (Exception ex) catch (Exception ex)
@ -62,6 +66,10 @@ namespace FastGithub.DomainResolve
this.logger.LogWarning($"{dns}无法解析{domain}{ex.Message}"); this.logger.LogWarning($"{dns}无法解析{domain}{ex.Message}");
return Array.Empty<IPAddress>(); return Array.Empty<IPAddress>();
} }
finally
{
semaphore.Release();
}
} }
/// <summary> /// <summary>