Skip to content

Commit c260c66

Browse files
committed
feat: 使用异步提高性能
1 parent b44b601 commit c260c66

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/components/BootstrapBlazor.IP2Region/Services/IP2RegionService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2Re
3131
private readonly IOptions<BootstrapBlazorOptions> _options;
3232
private readonly IOptions<IP2RegionOptions> _ipOptions;
3333
private readonly ILogger<IP2RegionService> _logger;
34+
private readonly TaskCompletionSource _tcs = new();
3435
private Searcher? _search;
3536

3637
/// <summary>
3738
/// <inheritdoc/>
3839
/// </summary>
3940
/// <param name="ip"></param>
40-
protected override Task<string?> LocateByIp(string ip)
41+
protected override async Task<string?> LocateByIp(string ip)
4142
{
43+
await _tcs.Task;
4244
string? result = null;
4345
if (_search != null && _options.Value.WebClientOptions.EnableIpLocator)
4446
{
4547
result = _search.Search(ip);
4648
}
47-
return Task.FromResult(result);
49+
return result;
4850
}
4951

5052
private void InitSearch()
@@ -60,9 +62,11 @@ private void InitSearch()
6062
{
6163
_search = new Searcher(CachePolicy.Content, xdbPath);
6264
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
65+
_tcs.TrySetResult();
6366
}
6467
catch (Exception ex)
6568
{
69+
_tcs.TrySetException(ex);
6670
_logger.LogError(ex, "IP2Region xdb file path: {dbPath}", xdbPath);
6771
}
6872
}

0 commit comments

Comments
 (0)