程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 讀取純真IP數據庫的公用組件接口QQWry.NET

讀取純真IP數據庫的公用組件接口QQWry.NET

編輯:ASP.NET基礎
這是一個讀取純真IP數據庫的公用組件接口,我是通過luma的《純真IP數據庫格式詳解》了解了純真IP數據庫數據格式,並且基於網絡上的一個IPLocation.dll源碼的基礎改編而來。我為什麼要改編這個組件呢?因為我看到這個組件在使用過程中,每次都要打開文件流,並且整個接口使用靜態的屬性。並不適合Web環境下,多線程並發查詢的需求,並且在性能上也不是最優。有了luma的格式詳解,和現有的IPLocation.dll的源碼,使我的工作變得異常的簡單。出現的一個小錯誤,也是經過一次調試後就解決了。性能較IPLocation.dll也有較大的提高,雖然只有短短的幾百行代碼,雖然網上也有很多類似的代碼,但繼承我一貫的做法,我仍然把這個組件開源貢獻出來。下面是一些接口使用的介紹:
復制代碼 代碼如下:
QQWry.NET.QQWryLocator qqWry = new QQWry.NET.QQWryLocator("qqwry.dat");//初始化數據庫文件,並獲得IP記錄數,通過Count可以獲得
QQWry.NET.IPLocation ip = qqWry.Query("120.67.217.7"); //查詢一個IP地址
Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local);

以下是與IPLocation.dll在性能上的對比代碼:
復制代碼 代碼如下:
Stopwatch stopwatch = new Stopwatch();
List<string> ips = new List<string> { "218.5.3.128", "120.67.217.7", "125.78.67.175", "220.250.64.23", "218.5.3.128", "120.67.217.7", "125.78.67.175", "220.250.64.23" };
stopwatch.Start();
for (int i = 0; i < 100; i++)
{
foreach (string item in ips)
{
ip = qqWry.Query(item);
// Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local);
}
}
stopwatch.Stop();
Console.WriteLine("QQWryLocator 花了{0} ms", stopwatch.ElapsedMilliseconds);
stopwatch.Reset();
stopwatch.Start();
for (int i = 0; i < 100; i++)
{
foreach (string item in ips)
{
string s = IPLocation.IPLocation.IPLocate("qqwry.dat", item);
// Console.WriteLine(s);
}
}
stopwatch.Stop();
Console.WriteLine("IPLocation 花了{0} ms", stopwatch.ElapsedMilliseconds);

性能比較結果:
 
源碼和示例下載
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved