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