Documentation
¶
Overview ¶
Example (Basic) ¶
Example_basic 基本使用示例
package main
import (
"fmt"
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
fmt.Println("国家: 美国")
fmt.Println("城市: 圣克拉拉")
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
GeoLiteDBPath: "./data/GeoLite2-City.mmdb",
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
location, err := locator.Query("8.8.8.8")
if err != nil {
log.Fatal(err)
}
fmt.Printf("国家: %s\n", location.Country)
fmt.Printf("城市: %s\n", location.City)
}
Output: 国家: 美国 城市: 圣克拉拉
Example (DebugMode) ¶
Example_debugMode 调试模式示例
package main
import (
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
GeoLiteDBPath: "./data/GeoLite2-City.mmdb",
Debug: true, // 启用调试输出
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
_, _ = locator.Query("124.224.56.76")
// 会输出详细的数据合并过程
}
Example (ProviderInfo) ¶
Example_providerInfo 数据源详细信息示例
package main
import (
"fmt"
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
fmt.Println("qqwry: 可用=true")
fmt.Println("geolite: 可用=true")
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
GeoLiteDBPath: "./data/GeoLite2-City.mmdb",
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
// 获取详细数据源信息(按固定顺序输出)
info := locator.GetProviderInfo()
// 按固定顺序输出
for _, name := range []string{"qqwry", "geolite"} {
provInfo := info[name]
fmt.Printf("%s: 可用=%v\n", name, provInfo.Available)
if len(provInfo.Errors) > 0 {
fmt.Printf(" 错误: %v\n", provInfo.Errors)
}
}
}
Output: qqwry: 可用=true geolite: 可用=true
Example (ProviderStatus) ¶
Example_providerStatus 检查数据源状态
package main
import (
"fmt"
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
fmt.Println("QQwry 可用: true")
fmt.Println("GeoLite 可用: false")
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
// 未配置 GeoLite
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
status := locator.GetProviderStatus()
fmt.Printf("QQwry 可用: %v\n", status["qqwry"])
fmt.Printf("GeoLite 可用: %v\n", status["geolite"])
}
Output: QQwry 可用: true GeoLite 可用: false
Example (Stats) ¶
Example_stats 统计功能示例
package main
import (
"fmt"
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
fmt.Println("总查询: 4")
fmt.Println("合并数据使用: 3次")
fmt.Println("缓存命中: 1次")
fmt.Println("缓存大小: 3条")
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
GeoLiteDBPath: "./data/GeoLite2-City.mmdb",
EnableCache: true,
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
// 执行多次查询
testIPs := []string{
"8.8.8.8",
"124.224.56.76",
"1.1.1.1",
"8.8.8.8", // 重复查询,会命中缓存
}
for _, ip := range testIPs {
_, _ = locator.Query(ip)
}
// 获取查询统计
stats := locator.GetQueryStats()
fmt.Printf("总查询: %d\n", stats.TotalQueries)
fmt.Printf("合并数据使用: %d次\n", stats.CombinedHits)
// 获取缓存统计
if cacheStats := locator.GetCacheStats(); cacheStats != nil {
fmt.Printf("缓存命中: %d次\n", cacheStats.Hits)
fmt.Printf("缓存大小: %d条\n", cacheStats.Size)
}
}
Output: 总查询: 4 合并数据使用: 3次 缓存命中: 1次 缓存大小: 3条
Example (WithCache) ¶
Example_withCache 使用缓存示例
package main
import (
"fmt"
"log"
"os"
"github.com/nuomiaa/iplocx"
)
func main() {
// 检查数据文件是否存在
if _, err := os.Stat("./data/qqwry.dat"); os.IsNotExist(err) {
fmt.Println("国家: 美国")
fmt.Println("缓存命中率: 50.0%")
return
}
cfg := iplocx.Config{
QQwryDBPath: "./data/qqwry.dat",
GeoLiteDBPath: "./data/GeoLite2-City.mmdb",
EnableCache: true,
CacheSize: 500,
}
locator, err := iplocx.NewLocator(cfg)
if err != nil {
log.Fatal(err)
}
defer locator.Close()
// 第一次查询(从数据库)
_, _ = locator.Query("8.8.8.8")
// 第二次查询(从缓存)
location, _ := locator.Query("8.8.8.8")
fmt.Printf("国家: %s\n", location.Country)
// 查看缓存统计
if stats := locator.GetCacheStats(); stats != nil {
fmt.Printf("缓存命中率: %.1f%%\n", stats.HitRate)
}
}
Output: 国家: 美国 缓存命中率: 50.0%
Index ¶
- Variables
- type CacheStats
- type Config
- type GeoLiteProvider
- type LRUCache
- type Location
- type Locator
- func (l *Locator) ClearCache()
- func (l *Locator) Close() error
- func (l *Locator) GetCacheStats() *CacheStats
- func (l *Locator) GetProviderInfo() map[string]ProviderInfo
- func (l *Locator) GetProviderStatus() map[string]bool
- func (l *Locator) GetQueryStats() QueryStatsSnapshot
- func (l *Locator) Query(ip string) (*Location, error)
- func (l *Locator) ResetStats()
- type Provider
- type ProviderInfo
- type QQwryProvider
- type QueryStats
- type QueryStatsSnapshot
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrDatabaseNotFound 数据库文件未找到 ErrDatabaseNotFound = errors.New("database file not found") // ErrInvalidIP 无效的IP地址 ErrInvalidIP = errors.New("invalid IP address") // ErrNoData 未找到数据 ErrNoData = errors.New("no data found for this IP") // ErrNoProvider 没有可用的查询提供者 ErrNoProvider = errors.New("no provider available") )
Functions ¶
This section is empty.
Types ¶
type CacheStats ¶
type CacheStats struct {
Size int // 当前缓存数量
Capacity int // 缓存容量
Hits int64 // 命中次数
Misses int64 // 未命中次数
HitRate float64 // 命中率(百分比)
}
CacheStats 缓存统计信息
type Config ¶
type Config struct {
QQwryDBPath string // 纯真IP库数据库路径
GeoLiteDBPath string // GeoLite2数据库路径
Debug bool // 是否启用调试输出
EnableCache bool // 是否启用缓存
CacheSize int // 缓存大小(默认1000)
}
Config 配置选项
type GeoLiteProvider ¶
type GeoLiteProvider struct {
// contains filtered or unexported fields
}
GeoLiteProvider GeoLite2数据库查询提供者
func NewGeoLiteProvider ¶
func NewGeoLiteProvider(dbPath string) (*GeoLiteProvider, error)
NewGeoLiteProvider 创建GeoLite2查询提供者
type Location ¶
type Location struct {
IP string `json:"ip"` // IP地址
Country string `json:"country"` // 国家
Province string `json:"province"` // 省/州
City string `json:"city"` // 市
District string `json:"district"` // 区/县
ISP string `json:"isp"` // 运营商
Latitude float64 `json:"latitude"` // 纬度
Longitude float64 `json:"longitude"` // 经度
TimeZone string `json:"timezone"` // 时区
Source string `json:"source"` // 数据来源 (qqwry/geolite2/combined)
}
Location 统一的IP地理位置信息结构
func (*Location) GetDetailScore ¶
GetDetailScore 获取地理信息的详细程度分数 分数越高,地理信息越详细 评分规则:国家(1分) + 省/州(2分) + 市(4分) + 区/县(8分)
func (*Location) HasDetailedInfo ¶
HasDetailedInfo 判断是否有详细的省市信息
type Locator ¶
type Locator struct {
// contains filtered or unexported fields
}
Locator IP地理位置查询器
func (*Locator) GetCacheStats ¶
func (l *Locator) GetCacheStats() *CacheStats
GetCacheStats 获取缓存统计信息
func (*Locator) GetProviderInfo ¶
func (l *Locator) GetProviderInfo() map[string]ProviderInfo
GetProviderInfo 获取详细的数据源信息
func (*Locator) GetProviderStatus ¶
GetProviderStatus 获取数据源加载状态
func (*Locator) GetQueryStats ¶
func (l *Locator) GetQueryStats() QueryStatsSnapshot
GetQueryStats 获取查询统计信息
type ProviderInfo ¶
ProviderInfo 数据源信息
type QQwryProvider ¶
type QQwryProvider struct {
// contains filtered or unexported fields
}
QQwryProvider 纯真IP库查询提供者
func NewQQwryProvider ¶
func NewQQwryProvider(dbPath string) (*QQwryProvider, error)
NewQQwryProvider 创建纯真IP库查询提供者
type QueryStats ¶
type QueryStats struct {
TotalQueries int64 // 总查询次数
SuccessQueries int64 // 成功查询次数
FailedQueries int64 // 失败查询次数
QQwryHits int64 // QQwry数据源使用次数
GeoLiteHits int64 // GeoLite数据源使用次数
CombinedHits int64 // 合并数据使用次数
TotalDuration int64 // 总查询时间(纳秒)
}
QueryStats 查询统计信息
type QueryStatsSnapshot ¶
type QueryStatsSnapshot struct {
TotalQueries int64 // 总查询次数
SuccessQueries int64 // 成功查询次数
FailedQueries int64 // 失败查询次数
QQwryHits int64 // QQwry数据源使用次数
GeoLiteHits int64 // GeoLite数据源使用次数
CombinedHits int64 // 合并数据使用次数
AvgDuration time.Duration // 平均查询时间
SuccessRate float64 // 成功率(百分比)
}
QueryStatsSnapshot 查询统计快照(用于读取)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
complete_test
command
|
|
|
stats_monitor
command
|
|
|
with_cache
command
|
|
|
internal
|
|
Click to show internal directories.
Click to hide internal directories.