阿不

潜水

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

这是一个读取纯真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在性能上的对比代码:
   1: Stopwatch stopwatch = new Stopwatch();
   2: 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" };
   3: stopwatch.Start();
   4: for (int i = 0; i < 100; i++)
   5: {
   6:    foreach (string item in ips)
   7:    {
   8:         ip = qqWry.Query(item);
   9:       // Console.WriteLine("{0} {1} {2}", ip.IP, ip.Country, ip.Local);
  10:    }
  11: }
  12:  
  13: stopwatch.Stop();
  14: Console.WriteLine("QQWryLocator 花了{0} ms", stopwatch.ElapsedMilliseconds);
  15:  
  16: stopwatch.Reset();
  17: stopwatch.Start();
  18: for (int i = 0; i < 100; i++)
  19: {
  20:    foreach (string item in ips)
  21:    {
  22:        string s = IPLocation.IPLocation.IPLocate("qqwry.dat", item);
  23:       // Console.WriteLine(s);
  24:    }
  25: }
  26: stopwatch.Stop();
  27: Console.WriteLine("IPLocation 花了{0} ms", stopwatch.ElapsedMilliseconds);

性能比较结果:

image 

源码和示例下载

阿不

posted on 2008-06-19 20:25  阿不  阅读(13125)  评论(42编辑  收藏  举报