Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
244472d9
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 大约 1 年
通知
21
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Ip2region
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
244472d9
编写于
6月 20, 2022
作者:
L
lion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add docs for cache searcher create
上级
e9a033b9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
52 addition
and
5 deletion
+52
-5
binding/golang/ReadMe.md
binding/golang/ReadMe.md
+52
-5
未找到文件。
binding/golang/ReadMe.md
浏览文件 @
244472d9
...
...
@@ -7,7 +7,8 @@
go get github.com/lionsoul2014/ip2region/binding/golang
```
### API 使用 Demo
### 完全基于文件的查询
```
golang
import
(
"fmt"
...
...
@@ -17,11 +18,11 @@ import (
func
main
()
{
var
dbPath
=
"ip2region.xdb file path"
searcher
,
err
:=
xdb
.
New
(
dbPath
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to create searcher: %s
\n
"
,
err
.
Error
())
searcher
,
err
:=
xdb
.
New
WithFileOnly
(
dbPath
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to create searcher: %s
\n
"
,
err
.
Error
())
return
}
}
defer
searcher
.
Close
()
...
...
@@ -35,9 +36,55 @@ func main() {
}
fmt
.
Printf
(
"{region: %s, took: %s}
\n
"
,
region
,
time
.
Since
(
tStart
))
// 备注:并发使用,每个 goroutine 需要创建一个独立的 searcher 对象。
}
```
### 缓存 `VetorIndex` 索引
可以预先加载 vecotorIndex 缓存,然后做成全局变量,每次创建 searcher 的时候使用全局的 vectorIndex,可以减少一次固定的 IO 操作从而加速查询减少系统 io 压力。
```
golang
// 1、从 dbPath 加载 VectorIndex 缓存,把下述 vIndex 变量全局到内存里面。
vIndex
,
err
:=
LoadVectorIndexFromFile
(
dbPath
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to load vector index from `%s`: %s
\n
"
,
dbPath
,
err
)
return
}
// 2、用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
searcher
,
err
:=
xdb
.
NewWithVectorIndex
(
dbPath
,
vIndex
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to create searcher with vector index: %s
\n
"
,
err
)
return
}
// 备注:并发使用,全部 goroutine 共享全局的只读 vIndex 缓存,每个 goroutine 创建一个独立的 searcher 对象
```
### 缓存整个 xdb 数据
可以预先加载整个 ip2region.xdb 到内存,完全基于内存查询,类似于之前的 memory search 查询。
```
golang
// 1、从 dbPath 加载整个 xdb 到内存
cBuff
,
err
:=
LoadContentFromFile
(
dbPath
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to load content from `%s`: %s
\n
"
,
dbPath
,
err
)
return
}
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
searcher
,
err
:=
xdb
.
NewWithBuffer
(
cBuff
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to create searcher with content: %s
\n
"
,
err
)
return
}
// 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
```
# 编译测试程序
通过如下方式编译得到 xdb_searcher 可执行程序:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录