新增物流接口

This commit is contained in:
zhongqiang 2025-06-23 16:00:54 +08:00
parent 12db943012
commit 9eecda8c5f
3 changed files with 37 additions and 6 deletions

View File

@ -97,9 +97,9 @@ func (a *AliCloudExpressClient) Set(cache ICacheAdapter) {
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 {
res, err = a.cache.Get(ctx, a.numberKey(number))
res, err = a.cache.Get(ctx, a.numberKey(prefix, number))
if err != nil {
return nil, errors.Wrapf(err, "获取缓存失败, number:%s", number)
}
@ -119,7 +119,7 @@ func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, m
}
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 {
return nil, errors.Wrapf(err, "缓存物流信息失败, number:%s", number)
}
@ -127,9 +127,17 @@ func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, m
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
func (a *AliCloudExpressClient) numberKey(number string) string {
return fmt.Sprintf("app:number:%s", number)
func (a *AliCloudExpressClient) numberKey(prefix, number string) string {
return fmt.Sprintf("%s:number:%s", prefix, number)
}
type ExpressRes struct {

View File

@ -65,14 +65,36 @@ func TestAliCloudExpressClient_GetLogisticsInfoFormCache(t *testing.T) {
Host: "",
cache: cache,
}
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", time.Minute)
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", "", time.Minute)
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 {
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 {
return &RedisCache{client: client}
}

View File

@ -8,4 +8,5 @@ import (
type ICacheAdapter interface {
Set(ctx context.Context, number string, res string, ttl time.Duration) error
Get(ctx context.Context, number string) (*ExpressRes, error)
Del(ctx context.Context, number string) error
}