Skip to content

Commit

Permalink
性能:移除IP格式检查,正则性能问题,提升10倍速度
Browse files Browse the repository at this point in the history
  • Loading branch information
JadynWong committed Jun 1, 2020
1 parent 0b52c02 commit 65cd135
Show file tree
Hide file tree
Showing 9 changed files with 1,077 additions and 44 deletions.
31 changes: 28 additions & 3 deletions QQWry.DependencyInjection/QQWry.DependencyInjection.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>JadynWong</Copyright>
<PackageProjectUrl>https://github.com/JadynWong/IP_qqwry</PackageProjectUrl>
Expand All @@ -12,8 +12,33 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.1" />
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
</ItemGroup>


<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 34 additions & 0 deletions QQWry/IpDbAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace QQWry
{
internal class IpDbAccessor
{
private byte[] InnerBytes;

public long Position { get; set; }

public IpDbAccessor(byte[] bytes)
{
InnerBytes = bytes;
}

public byte ReadByte()
{
var ret = InnerBytes[Position];
Position++;
return ret;
}

public void Read(byte[] buffer, int offset, int count)
{
for (var i = 0; i < count; i++)
{
buffer[offset + i] = ReadByte();
}
}
}
}
4 changes: 2 additions & 2 deletions QQWry/QQWry.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.5</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net4.5</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>JadynWong</Copyright>
Expand All @@ -22,7 +22,7 @@
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<ItemGroup Condition="'$(TargetFramework)' != 'net4.5'">
<PackageReference Include="System.Text.Encoding.CodePages">
<Version>4.7.0</Version>
</PackageReference>
Expand Down
41 changes: 18 additions & 23 deletions QQWry/QQWryIpSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ namespace QQWry
/// </summary>
public class QQWryIpSearch : IDisposable, IIpSearch
{
//public const string CopyWriteUrl = "http://update.cz88.net/ip/copywrite.rar";
//public const string QQWryUrl = "http://update.cz88.net/ip/qqwry.rar";

///// <summary>
///// 初始化锁定对象
///// </summary>
//private static readonly object _lockInit = new object();
///// <summary>
///// 读取锁定对象
///// </summary>
//private readonly object _lockRead = new object();

private readonly SemaphoreSlim _initLock = new SemaphoreSlim(initialCount: 1, maxCount: 1);

private object _versionLock = new object();
Expand All @@ -41,18 +29,22 @@ public class QQWryIpSearch : IDisposable, IIpSearch
/// 数据库 缓存
/// </summary>
private byte[] _qqwryDbBytes;

/// <summary>
/// Ip索引 缓存
/// </summary>
private long[] _ipIndexCache;

/// <summary>
/// 起始定位
/// </summary>
private long _startPosition;

/// <summary>
/// 是否初始化
/// </summary>
private bool? _init;

/// <summary>
/// IP地址正则验证
/// </summary>
Expand All @@ -61,7 +53,9 @@ public class QQWryIpSearch : IDisposable, IIpSearch
private static readonly HttpClient _httpClient;

private readonly QQWryOptions _qqwryOptions;

private int? _ipCount;

private string _version;

/// <inheritdoc />
Expand Down Expand Up @@ -108,8 +102,9 @@ public string Version

static QQWryIpSearch()
{
#if NETSTANDARD2_0
#if NET45

#else
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
_httpClient = new HttpClient();
Expand Down Expand Up @@ -222,10 +217,10 @@ public virtual IpLocation GetIpLocation(string strIp)
Ip = strIp
};

if (!CheckIp(strIp))
{
return loc;
}
//if (!CheckIp(strIp))
//{
// return loc;
//}

var ip = IpToLong(strIp);

Expand Down Expand Up @@ -255,7 +250,7 @@ public virtual IpLocation GetIpLocation(string strIp)
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public virtual async Task<bool> InitAsync(bool getNewDb = false, CancellationToken token = default(CancellationToken))
public virtual async Task<bool> InitAsync(bool getNewDb = false, CancellationToken token = default)
{
if (_init != null && !getNewDb)
{
Expand Down Expand Up @@ -337,16 +332,16 @@ public virtual async Task<QQWryCopyWrite> GetCopyWriteAsync()
/// <param name="strIp">要查询的IP地址</param>
/// <param name="token"></param>
/// <returns></returns>
public virtual async Task<IpLocation> GetIpLocationAsync(string strIp, CancellationToken token = default(CancellationToken))
public virtual async Task<IpLocation> GetIpLocationAsync(string strIp, CancellationToken token = default)
{
var loc = new IpLocation
{
Ip = strIp
};
if (!CheckIp(strIp))
{
return loc;
}
//if (!CheckIp(strIp))
//{
// return loc;
//}
long ip = IpToLong(strIp);
if (ip == IpToLong("127.0.0.1"))
{
Expand Down
Loading

0 comments on commit 65cd135

Please sign in to comment.