Golang使用 ip2region 查询IP的地区信息

devtools/2025/4/1 6:28:33/

利用 ip2region 进行 IP 地址定位

import ("fmt""log""github.com/lionsoul2014/ip2region/binding/golang/xdb"
)func main() {ip := "213.118.179.98"dbPath := ".\\cmd\\ip\\ip2region.xdb"// 1、初始化查询器//searcher, err := searcherByFile(dbPath)//searcher, err := searcherByVectorIndex(dbPath)searcher, err := searcherByBuffer(dbPath)// 2、执行查询resp, err := searcher.SearchByStr(ip)if err != nil {log.Fatal("failed to search ip", err)}fmt.Printf("IP: %s, Location: %s\n", ip, resp)
}

方式1:完全基于文件的查询

func searcherByFile(dbPath string) (*xdb.Searcher, error) {searcher, err := xdb.NewWithFileOnly(dbPath)if err != nil {log.Println("Failed to load ip2region database", err)return nil, err}return searcher, nil
}

特点

  • 内存占用少:这种方法不会将数据库文件加载到内存,而是在查询时直接从磁盘读取文件,所以内存占用非常少。
  • 查询速度较慢:由于每次查询都需要进行磁盘 I/O 操作,所以查询速度相对较慢,尤其是在高并发场景下,频繁的磁盘 I/O 可能会成为性能瓶颈。
  • 并发使用限制:和 searcherByVectorIndex 方法一样,并发使用时每个 goroutine 都要创建独立的 searcher 对象。

方式2:缓存 VectorIndex 索引

func searcherByVectorIndex(dbPath string) (*xdb.Searcher, error) {vIndex, err := xdb.LoadVectorIndexFromFile("ip2region.xdb")if err != nil {log.Println("Failed to load vector index", err)return nil, err}searcher, err := xdb.NewWithVectorIndex(dbPath, vIndex)if err != nil {log.Println("Failed to create searcher with vector index", err)return nil, err}return searcher, nil
}

特点

  • 性能优化:借助加载 VectorIndex 缓存,查询时能减少磁盘 I/O 操作。VectorIndex 是一种索引结构,可快速定位到可能包含目标 IP 的数据块,从而加快查询速度。
  • 内存占用适中:相较于将整个数据库加载到内存,仅加载 VectorIndex 缓存占用的内存较少,不过比直接从文件查询时占用的内存多。
  • 并发使用限制:并发使用时,每个 goroutine 都要创建独立的 searcher 对象,因为 searcher 并非线程安全的。

方式3:缓存整个数据库

func searcherByBuffer(dbPath string) (*xdb.Searcher, error) {buff, err := xdb.LoadContentFromFile(dbPath)if err != nil {log.Println("Failed to load ip2region database", err)return nil, err}searcher, err := xdb.NewWithBuffer(buff)if err != nil {fmt.Println("failed to create searcher with content", err)return nil, err}return searcher, nil
}

特点

  • 查询速度最快:该方法把整个数据库文件加载到内存中,查询时无需进行磁盘 I/O 操作,直接在内存中进行查找,因此查询速度最快。
  • 内存占用大:需要将整个数据库文件加载到内存,所以内存占用较大。如果数据库文件较大,可能会对系统内存造成压力。
  • 并发安全:使用整个数据库缓存创建的 searcher 对象可以安全地用于并发,多个 goroutine 可以共享同一个 searcher 对象,无需为每个 goroutine 创建独立的 searcher 对象。

总结

  • 若系统内存有限,对查询速度要求不高,可选用 searcherByFile 方法。
  • 若希望在内存占用和查询速度之间取得平衡,可采用 searcherByVectorIndex 方法。
  • 若系统内存充足,对查询速度要求极高,且有高并发查询需求,可使用 searcherByBuffer 方法。

http://www.ppmy.cn/devtools/172238.html

相关文章

微服务面试题:配置中心

🧑 博主简介:CSDN博客专家,历代文学网(PC端可以访问:https://literature.sinhy.com/#/?__c1000,移动端可微信小程序搜索“历代文学”)总架构师,15年工作经验,精通Java编…

前端知识点---用正则表达式判断邮箱(javascript)

// 全面的正则&#xff08;兼容大多数情况&#xff09; const emailRegex /^[a-zA-Z0-9._%-][a-zA-Z0-9.-]\.[a-zA-Z]{2,}$/;// 或直接使用浏览器内置验证 <input type"email" required>/&#xff1a;正则表达式的起始和结束标志。 ^&#xff1a;匹配字符串的…

基础认证-判断题

判断题 1.在http模块中,多个请求可以使用同一个httpRequest对象,httpRequest对象可以复用。(错误) 2.订阅dataReceiverProgress响应事件是用来接收HTTP流式响应数据。(错误) 3.ArkTS中变量声明时不需要指定相应的类型 (错误) 4.UIAbility组件热启动时会触发onCreate()函…

数仓架构告别「补丁」时代!全新批流一体 Domino 架构终结“批流缝合”

在数字化转型的浪潮中&#xff0c;企业对数据处理的需求日益复杂多变&#xff0c;传统的批处理和流处理架构已难以满足日益增长的性能和时效性要求。在此背景下&#xff0c;YMatrix CEO 姚延栋发布了深度文章《数仓架构告别「补丁」时代&#xff01;全新批流一体 Domino 架构终…

物联网(IoT)系统中,数据采集器拿来即用

在物联网(IoT)系统中,数据采集器(也称为网关或数据集中器)扮演着至关重要的角色,主要负责从各种传感器和设备中收集数据,并将其转换为统一的格式后传输到云端或本地服务器进行处理和分析。以下是关于数据采集器的设计要点、功能需求以及实现方案: 一、数据采集器的核心…

ai-api-union项目,适配各AI厂商api

项目地址&#xff1a;alpbeta/ai-api-union 需求&#xff1a;实现兼容各大模型厂商api的流式对话和同步对话接口&#xff0c;本项目现兼容智谱、豆包、通义、通义版deepseek 设计 一个ChatController类对外暴露这两个接口&#xff0c;入参都为ChatRequest请求类&#xff0c;…

ubuntu常用命令详解

以下是一些常用的Ubuntu命令的详细解释&#xff1a; ls&#xff1a;列出当前目录下的文件和文件夹。 示例&#xff1a;ls cd&#xff1a;切换到指定目录。 示例&#xff1a;cd /path/to/directory pwd&#xff1a;显示当前所在的目录路径。 示例&#xff1a;pwd mkdir&#…

ExpTimerApcRoutine函数分析之作用是ActiveTimerListHead里面移除定时器_etimer

第一部分&#xff1a; VOID ExpTimerApcRoutine ( IN PKAPC Apc, IN PKNORMAL_ROUTINE *NormalRoutine, IN PVOID *NormalContext, IN PVOID *SystemArgument1, IN PVOID *SystemArgument2 ) /* Routine Description: This function is the special …