feature/ip优化
This commit is contained in:
parent
6d3891390f
commit
169996cc8a
@ -102,7 +102,13 @@ func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string) (re
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "获取ip:%s信息失败,", ip)
|
return nil, errors.Wrapf(err, "获取ip:%s信息失败,", ip)
|
||||||
}
|
}
|
||||||
err = h.cache.Set(ctx, h.ipKey(ip), *info, 24*time.Hour)
|
|
||||||
|
infoJson, err := json.Marshal(info)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "无法将IP信息转换为JSON,ip:%s", ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), 24*time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
||||||
}
|
}
|
||||||
|
@ -75,17 +75,12 @@ func NewRedisCache(client *redis.Client) *RedisCache {
|
|||||||
return &RedisCache{client: client}
|
return &RedisCache{client: client}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisCache) Set(ctx context.Context, ip string, info ApiResult, ttl time.Duration) error {
|
func (r *RedisCache) Set(ctx context.Context, ip string, info string, ttl time.Duration) error {
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
return errors.New("ip不能为空")
|
return errors.New("ip不能为空")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.Marshal(info)
|
return r.client.Set(ctx, ip, info, ttl).Err()
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("无法封送IP信息: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.client.Set(ctx, ip, data, ttl).Err()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get 从Redis获取IP信息
|
// Get 从Redis获取IP信息
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// Cache 定义缓存接口,遵循接口隔离原则
|
// Cache 定义缓存接口,遵循接口隔离原则
|
||||||
type ICacheAdapter interface {
|
type ICacheAdapter interface {
|
||||||
Set(ctx context.Context, ip string, info ApiResult, ttl time.Duration) error
|
Set(ctx context.Context, ip string, info string, ttl time.Duration) error
|
||||||
Get(ctx context.Context, ip string) (*ApiResult, error)
|
Get(ctx context.Context, ip string) (*ApiResult, error)
|
||||||
Exists(ctx context.Context, ip string) (bool, error)
|
Exists(ctx context.Context, ip string) (bool, error)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user