mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
Merge branch 'master' into fr_lua_ipv6
This commit is contained in:
commit
baa9f62d6b
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
@ -17,8 +17,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\..\data\ip2region.xdb">
|
||||
<Link>IP2Region/ip2region.xdb</Link>
|
||||
<Content Include="..\..\..\data\ip2region_v4.xdb">
|
||||
<Link>IP2Region/ip2region_v4.xdb</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
@ -7,7 +7,7 @@ BenchmarkRunner.Run(typeof(Program).Assembly);
|
||||
|
||||
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 _vectorSearcher = new Searcher(CachePolicy.VectorIndex,XdbPath);
|
||||
private readonly ISearcher _fileSearcher = new Searcher(CachePolicy.File,XdbPath);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
@ -9,11 +9,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
||||
<PackageReference Include="NUnit" Version="4.4.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
|
||||
<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>
|
||||
@ -21,12 +27,12 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\..\..\data\ip.merge.txt">
|
||||
<Link>TestData/ip.merge.txt</Link>
|
||||
<Content Include="..\..\..\data\ipv4_source.txt">
|
||||
<Link>TestData/ipv4_source.txt</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="..\..\..\data\ip2region.xdb">
|
||||
<Link>TestData/ip2region.xdb</Link>
|
||||
<Content Include="..\..\..\data\ip2region_v4.xdb">
|
||||
<Link>TestData/ip2region_v4.xdb</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
@ -5,7 +5,7 @@ namespace IP2Region.Net.Test;
|
||||
[TestFixture]
|
||||
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()
|
||||
{
|
||||
@ -49,7 +49,7 @@ public class SearcherTest
|
||||
public void TestBenchSearch(CachePolicy cachePolicy)
|
||||
{
|
||||
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))
|
||||
{
|
||||
|
||||
8
binding/csharp/IP2Region.Net.slnx
Normal file
8
binding/csharp/IP2Region.Net.slnx
Normal 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>
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<id>IP2Region.Net</id>
|
||||
<version>2.0.2</version>
|
||||
<version>2.1.0</version>
|
||||
<title>IP2Region.Net</title>
|
||||
<authors>Alan Lee</authors>
|
||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||
@ -15,17 +15,44 @@
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<UserSecretsId>c2f07fe1-a300-4de3-8200-1278ed8cb5b7</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\README.md" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<PackageReference Include="System.Buffers" Version="4.5.1" />
|
||||
<PackageReference Include="System.Memory" Version="4.5.5" />
|
||||
</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>
|
||||
<None Include="..\CHANGELOG.md">
|
||||
<Link>CHANGELOG.md</Link>
|
||||
|
||||
@ -30,28 +30,33 @@ Generate using [maker](https://github.com/lionsoul2014/ip2region/tree/master/mak
|
||||
## ASP.NET Core Usage
|
||||
|
||||
```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
|
||||
|
||||
``` ini
|
||||
|
||||
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
|
||||
.NET SDK=7.0.306
|
||||
[Host] : .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 |
|
||||
|-------------------------|-----------:|---------:|---------:|
|
||||
| CachePolicy_Content | 155.7 ns | 0.46 ns | 0.39 ns |
|
||||
| CachePolicy_File | 2,186.8 ns | 34.27 ns | 32.06 ns |
|
||||
| CachePolicy_VectorIndex | 1,570.3 ns | 27.53 ns | 22.99 ns |
|
||||
|
||||
|
||||
| Method | Mean | Error | StdDev |
|
||||
|------------------------ |-------------:|----------:|----------:|
|
||||
| CachePolicy_Content | 58.32 ns | 0.182 ns | 0.170 ns |
|
||||
| CachePolicy_File | 16,417.56 ns | 50.569 ns | 47.302 ns |
|
||||
| CachePolicy_VectorIndex | 9,348.11 ns | 38.492 ns | 65.363 ns |
|
||||
|
||||
## Contributing
|
||||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user