Merge branch 'master' into fr_lua_ipv6

This commit is contained in:
lion 2025-10-19 18:42:52 +08:00
commit baa9f62d6b
8 changed files with 110 additions and 31 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
@ -17,8 +17,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="..\..\..\data\ip2region.xdb"> <Content Include="..\..\..\data\ip2region_v4.xdb">
<Link>IP2Region/ip2region.xdb</Link> <Link>IP2Region/ip2region_v4.xdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

@ -7,7 +7,7 @@ BenchmarkRunner.Run(typeof(Program).Assembly);
public class CachePolicyCompare public class CachePolicyCompare
{ {
private static readonly string XdbPath = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region.xdb"); private static readonly string XdbPath = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region_v4.xdb");
private readonly ISearcher _contentSearcher = new Searcher(CachePolicy.Content, XdbPath); private readonly ISearcher _contentSearcher = new Searcher(CachePolicy.Content, XdbPath);
private readonly ISearcher _vectorSearcher = new Searcher(CachePolicy.VectorIndex,XdbPath); private readonly ISearcher _vectorSearcher = new Searcher(CachePolicy.VectorIndex,XdbPath);
private readonly ISearcher _fileSearcher = new Searcher(CachePolicy.File,XdbPath); private readonly ISearcher _fileSearcher = new Searcher(CachePolicy.File,XdbPath);

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
@ -9,11 +9,17 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" /> <PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" /> <PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" /> <PackageReference Include="NUnit.Analyzers" Version="4.10.0">
<PackageReference Include="coverlet.collector" Version="3.1.2" /> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -21,12 +27,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="..\..\..\data\ip.merge.txt"> <Content Include="..\..\..\data\ipv4_source.txt">
<Link>TestData/ip.merge.txt</Link> <Link>TestData/ipv4_source.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="..\..\..\data\ip2region.xdb"> <Content Include="..\..\..\data\ip2region_v4.xdb">
<Link>TestData/ip2region.xdb</Link> <Link>TestData/ip2region_v4.xdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

@ -5,7 +5,7 @@ namespace IP2Region.Net.Test;
[TestFixture] [TestFixture]
public class SearcherTest public class SearcherTest
{ {
private readonly string _xdbPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip2region.xdb"); private readonly string _xdbPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip2region_v4.xdb");
public static IEnumerable<string> Ips() public static IEnumerable<string> Ips()
{ {
@ -49,7 +49,7 @@ public class SearcherTest
public void TestBenchSearch(CachePolicy cachePolicy) public void TestBenchSearch(CachePolicy cachePolicy)
{ {
Searcher searcher = new Searcher(cachePolicy,_xdbPath); Searcher searcher = new Searcher(cachePolicy,_xdbPath);
var srcPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ip.merge.txt"); var srcPath = Path.Combine(AppContext.BaseDirectory, "TestData", "ipv4_source.txt");
foreach (var line in File.ReadLines(srcPath)) foreach (var line in File.ReadLines(srcPath))
{ {

View File

@ -0,0 +1,8 @@
<Solution>
<Folder Name="/Solution Items/">
<File Path="README.md" />
</Folder>
<Project Path="IP2Region.Net.BenchMark/IP2Region.Net.BenchMark.csproj" />
<Project Path="IP2Region.Net.Test/IP2Region.Net.Test.csproj" />
<Project Path="IP2Region.Net/IP2Region.Net.csproj" />
</Solution>

View File

@ -0,0 +1,33 @@
using IP2Region.Net.Abstractions;
using IP2Region.Net.XDB;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// IP2Region 服务扩展类
/// </summary>
public static class IP2RegionExtensions
{
/// <summary>
/// 添加 IP2RegionService 服务。
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/> 集合</param>
/// <param name="path">IP2Region 数据库文件的路径。</param>
/// <param name="cachePolicy">缓存策略,默认为 <see cref="CachePolicy.Content"/>。</param>
public static IServiceCollection AddIP2RegionService(this IServiceCollection services, string path, CachePolicy cachePolicy = CachePolicy.Content)
{
services.TryAddSingleton<ISearcher>(provider =>
{
return new Searcher(cachePolicy, path);
});
#if NET8_0_OR_GREATER
services.TryAddKeyedSingleton("IP2Region.Net", (provider, _) =>
{
return provider.GetRequiredService<ISearcher>();
});
#endif
return services;
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<id>IP2Region.Net</id> <id>IP2Region.Net</id>
<version>2.0.2</version> <version>2.1.0</version>
<title>IP2Region.Net</title> <title>IP2Region.Net</title>
<authors>Alan Lee</authors> <authors>Alan Lee</authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> <PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
@ -15,17 +15,44 @@
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks> <TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>10.0</LangVersion> <LangVersion>latest</LangVersion>
<UserSecretsId>c2f07fe1-a300-4de3-8200-1278ed8cb5b7</UserSecretsId> <UserSecretsId>c2f07fe1-a300-4de3-8200-1278ed8cb5b7</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" /> <None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Buffers" Version="4.5.1" /> <PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.5" /> <PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\CHANGELOG.md"> <None Include="..\CHANGELOG.md">
<Link>CHANGELOG.md</Link> <Link>CHANGELOG.md</Link>

View File

@ -30,28 +30,33 @@ Generate using [maker](https://github.com/lionsoul2014/ip2region/tree/master/mak
## ASP.NET Core Usage ## ASP.NET Core Usage
```csharp ```csharp
services.AddSingleton<ISearcher>(new Searcher(CachePolicy , "your xdb file path")); services.AddIP2RegionService("your xdb file path", cachePolicy: CachePolicy.Content);
```
NET6/7
```csharp
provider.GetRequiredService<ISearcher>()
```
NET8+ support keyed service
```csharp
provider.GetRequiredKeyedService<ISearcher>("IP2Region.Net");
``` ```
## Performance ## Performance
``` ini ``` ini
BenchmarkDotNet=v0.13.2, OS=macOS 13.4.1 (c) (22F770820d) [Darwin 22.5.0] BenchmarkDotNet=v0.13.2, OS=macOS 13.4.1 (c) (22F770820d) [Darwin 22.5.0]
Apple M1, 1 CPU, 8 logical and 8 physical cores Apple M1, 1 CPU, 8 logical and 8 physical cores
.NET SDK=7.0.306 .NET SDK=7.0.306
[Host] : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD [Host] : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD
DefaultJob : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD DefaultJob : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD
``` ```
| Method | Mean | Error | StdDev | | Method | Mean | Error | StdDev |
|-------------------------|-----------:|---------:|---------:| |------------------------ |-------------:|----------:|----------:|
| CachePolicy_Content | 155.7 ns | 0.46 ns | 0.39 ns | | CachePolicy_Content | 58.32 ns | 0.182 ns | 0.170 ns |
| CachePolicy_File | 2,186.8 ns | 34.27 ns | 32.06 ns | | CachePolicy_File | 16,417.56 ns | 50.569 ns | 47.302 ns |
| CachePolicy_VectorIndex | 1,570.3 ns | 27.53 ns | 22.99 ns | | CachePolicy_VectorIndex | 9,348.11 ns | 38.492 ns | 65.363 ns |
## Contributing ## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.