zoujingli / ip2region

准确率99.9%的ip地址定位库

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads PHP Version Require License

本库基于 ip2region 简单整合,方便 PHP 项目使用 Composer 来安装。

通过 Composer 安装

composer require zoujingli/ip2region

在项目中快速调用

$ip2region = new \Ip2Region();
$result = $ip2region->simple('8.8.8.8');
var_dump($result);

// 高级用法可以直接调用 XdbSearcher 类。

Ip2region 是什么

ip2region v2.0 - 是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现。

Ip2region 特性

1、标准化的数据格式

每个 ip 数据段的 region 信息都固定了格式:国家|区域|省份|城市|ISP,只有**的数据绝大部分精确到了城市,其他国家部分数据只能定位到国家,后前的选项全部是0。

2、数据去重和压缩

xdb 格式生成程序会自动去重和压缩部分数据,默认的全部 IP 数据,生成的 ip2region.xdb 数据库是 11MiB,随着数据的详细度增加数据库的大小也慢慢增大。

3、极速查询响应

即使是完全基于 xdb 文件的查询,单次查询响应时间在十微秒级别,可通过如下两种方式开启内存加速查询:

  1. vIndex 索引缓存 :使用固定的 512KiB 的内存空间缓存 vector index 数据,减少一次 IO 磁盘操作,保持平均查询效率稳定在10-20微秒之间。
  2. xdb 整个文件缓存:将整个 xdb 文件全部加载到内存,内存占用等同于 xdb 文件大小,无磁盘 IO 操作,保持微秒级别的查询效率。

4、IP 数据管理框架

v2.0 格式的 xdb 支持亿级别的 IP 数据段行数,region 信息也可以完全自定义,例如:你可以在 region 中追加特定业务需求的数据,例如:GPS信息/国际统一地域信息编码/邮编等。也就是你完全可以使用 ip2region 来管理你自己的 IP 定位数据。

xdb 数据查询

API 介绍,使用文档和测试程序请参考对应 searcher 查询客户端下的 ReadMe 介绍,全部查询 binding 实现情况如下:

Ok? 状态 编程语言 描述 贡献者
已完成 golang golang xdb 查询客户端实现 Lion
已完成 php php xdb 查询客户端实现 Lion
已完成 java java xdb 查询客户端实现 Lion
已完成 lua 纯 lua xdb 查询客户端实现 Lion
已完成 c ANSC c xdb 查询客户端实现 Lion
已完成 lua_c lua c 扩展 xdb 查询客户端实现 Lion
    待开始 rust rust xdb 查询客户端实现 Lion
已完成 python python xdb 查询客户端实现 厉害的花花
已完成 nodejs nodejs xdb 查询客户端实现 Wu Jian Ping
已完成 csharp csharp xdb 查询客户端实现 Alen Lee
    待开始 php_ext php c 扩展 xdb 查询客户端实现 待确定
    待开始 nginx nginx 扩展 xdb 查询客户端实现 待确定

xdb 数据生成

API 介绍,使用文档和测试程序请参考对应 maker 生成程序下的 ReadMe 介绍,全部生成 maker 实现情况如下:

Ok? 状态 编程语言 描述 贡献者
已完成 golang golang xdb 生成程序实现 Lion
已完成 java java xdb 生成程序实现 Lion
    待开始 c ANSC c xdb 生成程序实现 Lion
已完成 python python xdb 生成程序实现 leolin49
已完成 csharp csharp xdb 生成程序实现 Alan Lee

并发查询必读

全部查询客户端的 search 接口都 不是 并发安全的实现,不同进程/线程/协程需要通过创建不同的查询对象来安全使用,并发量很大的情况下,基于文件查询的方式可能会导致打开文件数过多的错误,请修改内核的最大允许打开文件数(fs.file-max=一个更高的值),或者将整个xdb加载到内存进行安全并发使用。

相关备注

1、使用声明

ip2region 重点在于研究 IP 定位数据的存储设计和各种语言的查询实现,并没有原始 IP 数据的支撑,本项目不保证及时的数据更新,没有也不会有商用版本,你可以使用自定义的数据导入 ip2region 进行管理。

2、技术交流

ip2region 微信交流群,请先加微信:lionsoul2014 (请备注 ip2region)

3、数据更新

基于检测算法的数据更新方式视频分享:数据更新实现视频分享 - part1数据更新实现视频分享 - part2

4、数据结构

  1. xdb 数据结构分析:“ip2region xdb 数据结构和查询过程详解“
  2. xdb 查询过程分析:“ip2region xdb 数据结构和查询过程详解”
  3. xdb 生成过程分析:“ip2region xdb 二进制数据生成过程详解”

关于 ip2region v2.0 的 PHP 用法

完全基于文件的查询

$dbFile = "ip2region.xdb file path";
try {
    $searcher = XdbSearcher::newWithFileOnly($dbFile);
} catch (Exception $e) {
    printf("failed to create searcher with '%s': %s\n", $dbFile, $e);
    return;
}

$ip = '1.2.3.4';
$sTime = XdbSearcher::now();
$region = $searcher->search($ip);
if ($region === null) {
    // something is wrong
    printf("failed search(%s)\n", $ip);
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象。

缓存 VectorIndex 索引

如果你的 php 母环境支持,可以预先加载 vectorIndex 缓存,然后做成全局变量,每次创建 Searcher 的时候使用全局的 vectorIndex,可以减少一次固定的 IO 操作从而加速查询,减少 io 压力。

// 1、从 dbPath 加载 VectorIndex 缓存,把下述的 vIndex 变量缓存到内存里面。
$vIndex = XdbSearcher::loadVectorIndexFromFile($dbPath);
if ($vIndex === null) {
    printf("failed to load vector index from '%s'\n", $dbPath);
    return;
}

// 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
try {
    $searcher = XdbSearcher::newWithVectorIndex($dbFile, $vIndex);
} catch (Exception $e) {
    printf("failed to create vectorIndex cached searcher with '%s': %s\n", $dbFile, $e);
    return;
}

// 3、查询
$sTime = XdbSearcher::now();
$region = $searcher->search('1.2.3.4');
if ($region === null) {
    printf("failed search(1.2.3.4)\n");
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注:并发使用,每个线程或者协程需要创建一个独立的 searcher 对象,但是都共享统一的只读 vectorIndex。

缓存整个 xdb 数据

如果你的 PHP 母环境支持,可以预先加载整个 xdb 的数据到内存,这样可以实现完全基于内存的查询,类似之前的 memory search 查询。

// 1、从 dbPath 加载整个 xdb 到内存。
$cBuff = XdbSearcher::loadContentFromFile($dbPath);
if ($cBuff === null) {
    printf("failed to load content buffer from '%s'\n", $dbPath);
    return;
}

// 2、使用全局的 cBuff 创建带完全基于内存的查询对象。
try {
    $searcher = XdbSearcher::newWithBuffer($cBuff);
} catch (Exception $e) {
    printf("failed to create buffer cached searcher: %s\n", $dbFile, $e);
    return;
}

// 3、查询
$sTime = XdbSearcher::now();
$region = $searcher->search('1.2.3.4');
if ($region === null) {
    printf("failed search(1.2.3.4)\n");
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。

查询测试

通过 search_test.php 脚本来进行查询测试:

➜  php git:(v2.0_xdb) ✗ php ./search_test.php
php ./search_test.php [command options]
options:
 --db string             ip2region binary xdb file path
 --cache-policy string   cache policy: file/vectorIndex/content

例如:使用默认的 data/ip2region.xdb 进行查询测试:

➜  php git:(v2.0_xdb) ✗ php ./search_test.php --db=../../data/ip2region.xdb --cache-policy=vectorIndex
ip2region xdb searcher test program, cachePolicy: vectorIndex
type 'quit' to exit
ip2region>> 1.2.3.4
{region: 美国|0|华盛顿|0|谷歌, ioCount: 7, took: 0.04492 ms}
ip2region>> 

输入 ip 即可进行查询测试。也可以分别设置 cache-policy 为 file/vectorIndex/content 来测试三种不同缓存实现的效率。

bench 测试

通过 bench_test.php 脚本来进行自动 bench 测试,一方面确保 xdb 文件没有错误,另一方面通过大量的查询测试平均查询性能:

➜  php git:(v2.0_xdb) ✗ php ./bench_test.php
php ./bench_test.php [command options]
options:
 --db string             ip2region binary xdb file path
 --src string            source ip text file path
 --cache-policy string   cache policy: file/vectorIndex/content

例如:通过默认的 data/ip2region.xdb 和 data/ip.merge.txt 来进行 bench 测试:

➜  php git:(v2.0_xdb) ✗ php ./bench_test.php --db=../../data/ip2region.xdb --src=../../data/ip.merge.txt --cache-policy=vectorIndex
Bench finished, {cachePolicy: vectorIndex, total: 3417955, took: 15s, cost: 0.005 ms/op}

可以通过设置 cache-policy 参数来分别测试 file/vectorIndex/content 三种不同的缓存实现的的性能。 @Note:请注意 bench 使用的 src 文件需要是生成对应的 xdb 文件的相同的源文件。

About

准确率99.9%的ip地址定位库

License:Apache License 2.0


Languages

Language:PHP 100.0%