diff --git a/pkg/models/file.go b/pkg/models/file.go index 2946bbe..599e4fc 100644 --- a/pkg/models/file.go +++ b/pkg/models/file.go @@ -5,7 +5,7 @@ import ( "pkg/utils" "dario.cat/mergo" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) type FileInfo struct { @@ -31,8 +31,8 @@ type RedisFileInfo struct { func GetRedisFileInfo(fileId string) (*RedisFileInfo, error) { rdb, ctx := utils.GetRedisClient() - fileInfoUnmarshalData, err := rdb.HGet(ctx, "015:fileInfoMap", fileId).Result() - if err == redis.Nil { + fileInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:fileInfoMap").Field(fileId).Build()).ToString() + if rueidis.IsRedisNil(err) { return nil, nil } if err != nil { @@ -55,11 +55,10 @@ func SetRedisFileInfo(fileId string, fileInfo RedisFileInfo) error { mergo.Merge(&fileInfo, old_fileInfo) } jsonData, _ := json.Marshal(fileInfo) - _, err = rdb.HSet(ctx, "015:fileInfoMap", fileId, string(jsonData)).Result() - return err + return rdb.Do(ctx, rdb.B().Hset().Key("015:fileInfoMap").FieldValue().FieldValue(fileId, string(jsonData)).Build()).Error() } func GetRedisFileInfoAll() (map[string]string, error) { rdb, ctx := utils.GetRedisClient() - return rdb.HGetAll(ctx, "015:fileInfoMap").Result() + return rdb.Do(ctx, rdb.B().Hgetall().Key("015:fileInfoMap").Build()).AsStrMap() } diff --git a/pkg/models/file_share_relational.go b/pkg/models/file_share_relational.go index 72f7923..3e4fece 100644 --- a/pkg/models/file_share_relational.go +++ b/pkg/models/file_share_relational.go @@ -4,13 +4,13 @@ import ( "encoding/json" "pkg/utils" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) func GetRedisFileShareRelational(fileId string) ([]string, error) { rdb, ctx := utils.GetRedisClient() - fileShareRelationalUnmarshalData, err := rdb.HGet(ctx, "015:fileShareRelational", fileId).Result() - if err == redis.Nil { + fileShareRelationalUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:fileShareRelational").Field(fileId).Build()).ToString() + if rueidis.IsRedisNil(err) { return nil, nil } if err != nil { @@ -26,6 +26,5 @@ func GetRedisFileShareRelational(fileId string) ([]string, error) { func SetRedisFileShareRelational(fileId string, shareIDs []string) error { rdb, ctx := utils.GetRedisClient() jsonData, _ := json.Marshal(shareIDs) - _, err := rdb.HSet(ctx, "015:fileShareRelational", fileId, string(jsonData)).Result() - return err + return rdb.Do(ctx, rdb.B().Hset().Key("015:fileShareRelational").FieldValue().FieldValue(fileId, string(jsonData)).Build()).Error() } diff --git a/pkg/models/pickupcode.go b/pkg/models/pickupcode.go index a2000cd..050daf0 100644 --- a/pkg/models/pickupcode.go +++ b/pkg/models/pickupcode.go @@ -6,13 +6,13 @@ import ( "pkg/utils" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) func GetRedisPickupData(pickupCode string) (string, error) { rdb, ctx := utils.GetRedisClient() - ShareId, err := rdb.Get(ctx, fmt.Sprintf("015:pickupCode:%s", pickupCode)).Result() - if err == redis.Nil { + ShareId, err := rdb.Do(ctx, rdb.B().Get().Key(fmt.Sprintf("015:pickupCode:%s", pickupCode)).Build()).ToString() + if rueidis.IsRedisNil(err) { return "", nil } if err != nil { @@ -23,6 +23,8 @@ func GetRedisPickupData(pickupCode string) (string, error) { func SetRedisPickupData(pickupCode string, shareId string) (bool, error) { rdb, ctx := utils.GetRedisClient() - ok, err := rdb.SetNX(ctx, fmt.Sprintf("015:pickupCode:%s", pickupCode), shareId, time.Until(time.Now().Add(24*time.Hour))).Result() - return ok, err + return rdb.Do( + ctx, + rdb.B().Set().Key(fmt.Sprintf("015:pickupCode:%s", pickupCode)).Value(shareId).Nx().Ex(24*time.Hour).Build(), + ).AsBool() } diff --git a/pkg/models/share.go b/pkg/models/share.go index 1b34911..3dd0b47 100644 --- a/pkg/models/share.go +++ b/pkg/models/share.go @@ -8,7 +8,7 @@ import ( "pkg/utils" "dario.cat/mergo" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) type RedisShareInfo struct { @@ -34,21 +34,21 @@ const ( func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) { rdb, ctx := utils.GetRedisClient() - shareInfo := rdb.Get(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId)) - shareInfoUnmarshalData, err := shareInfo.Result() - ttl, _ := rdb.TTL(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId)).Result() - if err == redis.Nil { + key := fmt.Sprintf("015:shareInfoMap:%s", shareId) + shareInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Get().Key(key).Build()).ToString() + if rueidis.IsRedisNil(err) { return nil, nil } if err != nil { return nil, err } + ttl, _ := rdb.Do(ctx, rdb.B().Ttl().Key(key).Build()).AsInt64() var shareInfoData RedisShareInfo if err := json.Unmarshal([]byte(shareInfoUnmarshalData), &shareInfoData); err != nil { return nil, err } - shareInfoData.ExpireAt = time.Now().Add(ttl).Unix() + shareInfoData.ExpireAt = time.Now().Add(time.Duration(ttl) * time.Second).Unix() return &shareInfoData, nil } @@ -62,6 +62,12 @@ func SetRedisShareInfo(shareId string, shareInfo RedisShareInfo) error { mergo.Merge(&shareInfo, old_shareInfo) } jsonData, _ := json.Marshal(shareInfo) - _, err = rdb.Set(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId), string(jsonData), time.Until(time.Unix(shareInfo.ExpireAt, 0))).Result() - return err + return rdb.Do( + ctx, + rdb.B().Set(). + Key(fmt.Sprintf("015:shareInfoMap:%s", shareId)). + Value(string(jsonData)). + Ex(time.Until(time.Unix(shareInfo.ExpireAt, 0))). + Build(), + ).Error() } diff --git a/pkg/models/stat.go b/pkg/models/stat.go index 263aaa7..6eab3dc 100644 --- a/pkg/models/stat.go +++ b/pkg/models/stat.go @@ -5,7 +5,7 @@ import ( "pkg/utils" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) // 统计数据结构 @@ -18,8 +18,8 @@ type StatData struct { func GetRedisStat(key string) (*StatData, error) { rdb, ctx := utils.GetRedisClient() - statUnmarshalData, err := rdb.HGet(ctx, "015:stat", key).Result() - if err == redis.Nil { + statUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:stat").Field(key).Build()).ToString() + if rueidis.IsRedisNil(err) { return nil, nil } if err != nil { @@ -48,11 +48,10 @@ func SetRedisStat(key string, handler func(stat *StatData) *StatData) error { } stat := handler(old_stat) jsonData, _ := json.Marshal(stat) - _, err = rdb.HSet(ctx, "015:stat", key, string(jsonData)).Result() - return err + return rdb.Do(ctx, rdb.B().Hset().Key("015:stat").FieldValue().FieldValue(key, string(jsonData)).Build()).Error() } func GetRedisStatAll() (map[string]string, error) { rdb, ctx := utils.GetRedisClient() - return rdb.HGetAll(ctx, "015:stat").Result() + return rdb.Do(ctx, rdb.B().Hgetall().Key("015:stat").Build()).AsStrMap() } diff --git a/pkg/models/task.go b/pkg/models/task.go index d350a04..a896ded 100644 --- a/pkg/models/task.go +++ b/pkg/models/task.go @@ -7,14 +7,13 @@ import ( "pkg/utils" - "github.com/redis/go-redis/v9" + "github.com/redis/rueidis" ) func GetRedisTaskInfo(taskId string) (*map[string]any, error) { rdb, ctx := utils.GetRedisClient() - taskInfo := rdb.Get(ctx, fmt.Sprintf("015:taskInfoMap:%s", taskId)) - taskInfoUnmarshalData, err := taskInfo.Result() - if err == redis.Nil { + taskInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Get().Key(fmt.Sprintf("015:taskInfoMap:%s", taskId)).Build()).ToString() + if rueidis.IsRedisNil(err) { return nil, nil } if err != nil { @@ -31,6 +30,8 @@ func GetRedisTaskInfo(taskId string) (*map[string]any, error) { func SetRedisTaskInfo(taskId string, taskInfo map[string]any) error { rdb, ctx := utils.GetRedisClient() jsonData, _ := json.Marshal(taskInfo) - _, err := rdb.Set(ctx, fmt.Sprintf("015:taskInfoMap:%s", taskId), jsonData, time.Hour).Result() - return err + return rdb.Do( + ctx, + rdb.B().Set().Key(fmt.Sprintf("015:taskInfoMap:%s", taskId)).Value(string(jsonData)).Ex(time.Hour).Build(), + ).Error() } diff --git a/worker/internal/tasks/file.go b/worker/internal/tasks/file.go index 3f081ad..804bc09 100644 --- a/worker/internal/tasks/file.go +++ b/worker/internal/tasks/file.go @@ -40,7 +40,9 @@ func RemoveFile(ctx context.Context, task *asynq.Task) error { return err } filePath := filepath.Join(uploadPath, payload.FileId) - rdb.HDel(rctx, "015:fileInfoMap", payload.FileId) + if err := rdb.Do(rctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil { + return err + } os.RemoveAll(filePath) return nil } diff --git a/worker/internal/tasks/share.go b/worker/internal/tasks/share.go index fe3a4dd..0b81bbb 100644 --- a/worker/internal/tasks/share.go +++ b/worker/internal/tasks/share.go @@ -36,8 +36,12 @@ func RemoveShare(ctx context.Context, task *asynq.Task) error { return err } filePath := filepath.Join(uploadPath, payload.FileId) - rdb.HDel(ctx, "015:fileShareRelational", payload.FileId) - rdb.HDel(ctx, "015:fileInfoMap", payload.FileId) + if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileShareRelational").Field(payload.FileId).Build()).Error(); err != nil { + return err + } + if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil { + return err + } os.RemoveAll(filePath) return nil }