新增物流接口
This commit is contained in:
parent
12db943012
commit
9eecda8c5f
@ -97,9 +97,9 @@ func (a *AliCloudExpressClient) Set(cache ICacheAdapter) {
|
|||||||
a.cache = cache
|
a.cache = cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, mobile, number string, opt ...time.Duration) (res *ExpressRes, err error) {
|
func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, mobile, prefix, number string, opt ...time.Duration) (res *ExpressRes, err error) {
|
||||||
if a.cache != nil {
|
if a.cache != nil {
|
||||||
res, err = a.cache.Get(ctx, a.numberKey(number))
|
res, err = a.cache.Get(ctx, a.numberKey(prefix, number))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "获取缓存失败, number:%s", number)
|
return nil, errors.Wrapf(err, "获取缓存失败, number:%s", number)
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, m
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(opt) > 0 {
|
if len(opt) > 0 {
|
||||||
err = a.cache.Set(ctx, a.numberKey(number), string(infoJson), opt[0])
|
err = a.cache.Set(ctx, a.numberKey(prefix, number), string(infoJson), opt[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "缓存物流信息失败, number:%s", number)
|
return nil, errors.Wrapf(err, "缓存物流信息失败, number:%s", number)
|
||||||
}
|
}
|
||||||
@ -127,9 +127,17 @@ func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, m
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AliCloudExpressClient) DeleteLogisticsInfoCache(ctx context.Context, prefix, number string) (err error) {
|
||||||
|
if a.cache == nil {
|
||||||
|
return errors.New("缓存不能为空")
|
||||||
|
}
|
||||||
|
err = a.cache.Del(ctx, a.numberKey(prefix, number))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ipKey 生成Redis key
|
// ipKey 生成Redis key
|
||||||
func (a *AliCloudExpressClient) numberKey(number string) string {
|
func (a *AliCloudExpressClient) numberKey(prefix, number string) string {
|
||||||
return fmt.Sprintf("app:number:%s", number)
|
return fmt.Sprintf("%s:number:%s", prefix, number)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExpressRes struct {
|
type ExpressRes struct {
|
||||||
|
@ -65,14 +65,36 @@ func TestAliCloudExpressClient_GetLogisticsInfoFormCache(t *testing.T) {
|
|||||||
Host: "",
|
Host: "",
|
||||||
cache: cache,
|
cache: cache,
|
||||||
}
|
}
|
||||||
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", time.Minute)
|
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", "", time.Minute)
|
||||||
log.Println(gotRes, err)
|
log.Println(gotRes, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAliCloudExpressClient_DeleteLogisticsInfoCache(t *testing.T) {
|
||||||
|
rdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "",
|
||||||
|
Password: "",
|
||||||
|
DB: 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
// 创建缓存实例
|
||||||
|
cache := NewRedisCache(rdb)
|
||||||
|
a := &AliCloudExpressClient{
|
||||||
|
AppCode: "",
|
||||||
|
Host: "",
|
||||||
|
cache: cache,
|
||||||
|
}
|
||||||
|
err := a.DeleteLogisticsInfoCache(context.Background(), "", "")
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
type RedisCache struct {
|
type RedisCache struct {
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *RedisCache) Del(ctx context.Context, number string) error {
|
||||||
|
return r.client.Del(ctx, number).Err()
|
||||||
|
}
|
||||||
|
|
||||||
func NewRedisCache(client *redis.Client) *RedisCache {
|
func NewRedisCache(client *redis.Client) *RedisCache {
|
||||||
return &RedisCache{client: client}
|
return &RedisCache{client: client}
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,5 @@ import (
|
|||||||
type ICacheAdapter interface {
|
type ICacheAdapter interface {
|
||||||
Set(ctx context.Context, number string, res string, ttl time.Duration) error
|
Set(ctx context.Context, number string, res string, ttl time.Duration) error
|
||||||
Get(ctx context.Context, number string) (*ExpressRes, error)
|
Get(ctx context.Context, number string) (*ExpressRes, error)
|
||||||
|
Del(ctx context.Context, number string) error
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user