mirror of
https://github.com/xiaoqidun/tipdw.git
synced 2024-11-23 11:26:46 +08:00
feat(支持缓存): 添加查询缓存
This commit is contained in:
parent
2a2830c4a6
commit
381a0f9cf6
12
tipdw.go
12
tipdw.go
@ -5,10 +5,14 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var client = &http.Client{Timeout: 5 * time.Second}
|
||||
var (
|
||||
client = &http.Client{Timeout: 5 * time.Second}
|
||||
ipCache = &sync.Map{}
|
||||
)
|
||||
|
||||
type Body struct {
|
||||
Status int `json:"status"` // 状态码,0为正常,其它为异常
|
||||
@ -37,6 +41,11 @@ type Location struct {
|
||||
|
||||
// QueryIP 使用腾讯位置服务查询IP
|
||||
func QueryIP(sk string, key string, ip string) (result Result, err error) {
|
||||
v, ok := ipCache.Load(ip)
|
||||
if ok {
|
||||
result = v.(Result)
|
||||
return
|
||||
}
|
||||
arg := &reqLBS{
|
||||
SK: sk,
|
||||
Path: "/ws/location/v1/ip",
|
||||
@ -68,5 +77,6 @@ func QueryIP(sk string, key string, ip string) (result Result, err error) {
|
||||
return
|
||||
}
|
||||
result = bodyUnmarshal.Result
|
||||
ipCache.Store(ip, bodyUnmarshal.Result)
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user