Skip to content

Commit e91fabf

Browse files
authored
feat(IPRegion): use async increase load speed (#536)
* doc: 格式化代码 * feat: 使用异步提高性能 * chore: bump version 9.0.3 * perf: 使用异步提高初始化速度
1 parent e14cf45 commit e91fabf

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/components/BootstrapBlazor.IP2Region/BootstrapBlazor.IP2Region.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.2</Version>
4+
<Version>9.0.3</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace BootstrapBlazor.Components;
1010

1111
/// <summary>
12-
/// 默认 IP2Region 实现
12+
/// 默认 IP2Region 实现
1313
/// </summary>
1414
class IP2RegionService : DefaultIpLocatorProvider
1515
{
@@ -25,26 +25,29 @@ public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2Re
2525
_ipOptions = ipRegionOptions;
2626
_logger = logger;
2727

28-
InitSearch();
28+
Task.Run(InitSearch, CancellationToken.None).ConfigureAwait(false);
2929
}
3030

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;
44+
4245
string? result = null;
4346
if (_search != null && _options.Value.WebClientOptions.EnableIpLocator)
4447
{
4548
result = _search.Search(ip);
4649
}
47-
return Task.FromResult(result);
50+
return result;
4851
}
4952

5053
private void InitSearch()
@@ -60,9 +63,11 @@ private void InitSearch()
6063
{
6164
_search = new Searcher(CachePolicy.Content, xdbPath);
6265
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
66+
_tcs.TrySetResult();
6367
}
6468
catch (Exception ex)
6569
{
70+
_tcs.TrySetException(ex);
6671
_logger.LogError(ex, "IP2Region xdb file path: {dbPath}", xdbPath);
6772
}
6873
}

0 commit comments

Comments
 (0)