diff --git a/express_tool/ali_cloud_client.go b/express_tool/ali_cloud_client.go index fe1438f..6ac3e2b 100644 --- a/express_tool/ali_cloud_client.go +++ b/express_tool/ali_cloud_client.go @@ -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 { diff --git a/express_tool/ali_cloud_client_test.go b/express_tool/ali_cloud_client_test.go index 688b7c2..d4ff0c3 100644 --- a/express_tool/ali_cloud_client_test.go +++ b/express_tool/ali_cloud_client_test.go @@ -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} } diff --git a/express_tool/express_cache.go b/express_tool/express_cache.go index 61e8e1c..18d3530 100644 --- a/express_tool/express_cache.go +++ b/express_tool/express_cache.go @@ -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 }