diff --git a/backend/main.go b/backend/main.go index c74f786..76a4938 100644 --- a/backend/main.go +++ b/backend/main.go @@ -18,6 +18,11 @@ func main() { } defer logger.Sync() //nolint:errcheck zap.ReplaceGlobals(logger) + // redis + if err := utils.InitRedis(); err != nil { + logger.Fatal("redis init failed", zap.Error(err)) + panic(err) + } e := echo.New() for _, middleware := range middlewares { diff --git a/pkg/models/file.go b/pkg/models/file.go index 10d5adc..2f5870c 100644 --- a/pkg/models/file.go +++ b/pkg/models/file.go @@ -1,6 +1,7 @@ package models import ( + "context" "encoding/json" "pkg/utils" "time" @@ -32,7 +33,8 @@ type RedisFileInfo struct { } func GetRedisFileInfo(fileId string) (*RedisFileInfo, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() fileInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:fileInfoMap").Field(fileId).Build()).ToString() if rueidis.IsRedisNil(err) { return nil, nil @@ -48,7 +50,8 @@ func GetRedisFileInfo(fileId string) (*RedisFileInfo, error) { } func SetRedisFileInfo(fileId string, handler func(fileInfo *RedisFileInfo) *RedisFileInfo) (*RedisFileInfo, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() old_fileInfo, err := GetRedisFileInfo(fileId) if err != nil { return nil, err @@ -72,6 +75,7 @@ func SetRedisFileInfo(fileId string, handler func(fileInfo *RedisFileInfo) *Redi } func GetRedisFileInfoAll() (map[string]string, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() 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 f7914bd..b06ec50 100644 --- a/pkg/models/file_share_relational.go +++ b/pkg/models/file_share_relational.go @@ -1,6 +1,7 @@ package models import ( + "context" "encoding/json" "pkg/utils" @@ -8,7 +9,8 @@ import ( ) func GetRedisFileShareRelational(fileId string) ([]string, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() fileShareRelationalUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:fileShareRelational").Field(fileId).Build()).ToString() if rueidis.IsRedisNil(err) { return nil, nil @@ -24,7 +26,8 @@ func GetRedisFileShareRelational(fileId string) ([]string, error) { } func SetRedisFileShareRelational(fileId string, shareIDs []string) error { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() jsonData, err := json.Marshal(shareIDs) if err != nil { return err diff --git a/pkg/models/pickupcode.go b/pkg/models/pickupcode.go index 050daf0..33a1301 100644 --- a/pkg/models/pickupcode.go +++ b/pkg/models/pickupcode.go @@ -1,6 +1,7 @@ package models import ( + "context" "fmt" "time" @@ -10,7 +11,8 @@ import ( ) func GetRedisPickupData(pickupCode string) (string, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() ShareId, err := rdb.Do(ctx, rdb.B().Get().Key(fmt.Sprintf("015:pickupCode:%s", pickupCode)).Build()).ToString() if rueidis.IsRedisNil(err) { return "", nil @@ -22,7 +24,8 @@ func GetRedisPickupData(pickupCode string) (string, error) { } func SetRedisPickupData(pickupCode string, shareId string) (bool, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() return rdb.Do( ctx, rdb.B().Set().Key(fmt.Sprintf("015:pickupCode:%s", pickupCode)).Value(shareId).Nx().Ex(24*time.Hour).Build(), diff --git a/pkg/models/share.go b/pkg/models/share.go index 246e454..aba936a 100644 --- a/pkg/models/share.go +++ b/pkg/models/share.go @@ -1,6 +1,7 @@ package models import ( + "context" "encoding/json" "fmt" "time" @@ -43,7 +44,8 @@ const ( ) func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() key := fmt.Sprintf("015:shareInfoMap:%s", shareId) shareInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Get().Key(key).Build()).ToString() if rueidis.IsRedisNil(err) { @@ -63,7 +65,8 @@ func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) { } func SetRedisShareInfo(shareId string, handler func(shareInfo *RedisShareInfo) *RedisShareInfo) (*RedisShareInfo, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() old_shareInfo, err := GetRedisShareInfo(shareId) if err != nil { return nil, err diff --git a/pkg/models/stat.go b/pkg/models/stat.go index 3bbebfd..fd0fccd 100644 --- a/pkg/models/stat.go +++ b/pkg/models/stat.go @@ -18,7 +18,8 @@ type StatData struct { } func GetRedisStat(key string) (*StatData, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() statUnmarshalData, err := rdb.Do(ctx, rdb.B().Hget().Key("015:stat").Field(key).Build()).ToString() if rueidis.IsRedisNil(err) { return nil, nil @@ -36,7 +37,7 @@ func GetRedisStat(key string) (*StatData, error) { func SetRedisStat(key string, handler func(stat *StatData) *StatData) (*StatData, error) { var updatedStat *StatData err := utils.WithLocker(context.Background(), "015:stat:"+key, 0, func(ctx context.Context) error { - rdb, _ := utils.GetRedisClient() + rdb := utils.GetRedisClient() old_stat, err := GetRedisStat(key) if err != nil { return err @@ -62,6 +63,7 @@ func SetRedisStat(key string, handler func(stat *StatData) *StatData) (*StatData } func GetRedisStatAll() (map[string]string, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() 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 4531c9e..984c040 100644 --- a/pkg/models/task.go +++ b/pkg/models/task.go @@ -1,6 +1,7 @@ package models import ( + "context" "encoding/json" "fmt" "time" @@ -11,7 +12,8 @@ import ( ) func GetRedisTaskInfo(taskId string) (*map[string]any, error) { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() taskInfoUnmarshalData, err := rdb.Do(ctx, rdb.B().Get().Key(fmt.Sprintf("015:taskInfoMap:%s", taskId)).Build()).ToString() if rueidis.IsRedisNil(err) { return nil, nil @@ -28,7 +30,8 @@ func GetRedisTaskInfo(taskId string) (*map[string]any, error) { } func SetRedisTaskInfo(taskId string, taskInfo map[string]any) error { - rdb, ctx := utils.GetRedisClient() + rdb := utils.GetRedisClient() + ctx := context.Background() jsonData, err := json.Marshal(taskInfo) if err != nil { return err diff --git a/pkg/utils/redis.go b/pkg/utils/redis.go index c7fc169..e38fb86 100644 --- a/pkg/utils/redis.go +++ b/pkg/utils/redis.go @@ -1,33 +1,24 @@ package utils import ( - "context" - "sync" - "github.com/redis/rueidis" ) -var ( - rdb rueidis.Client - ctx = context.Background() - onceRedis sync.Once -) +var rdb rueidis.Client -func InitRedis() rueidis.Client { +func InitRedis() error { opt, err := rueidis.ParseURL(GetEnv("redis.url")) if err != nil { - panic(err) + return err } client, err := rueidis.NewClient(opt) if err != nil { - panic(err) + return err } - return client + rdb = client + return nil } -func GetRedisClient() (rueidis.Client, context.Context) { - onceRedis.Do(func() { - rdb = InitRedis() - }) - return rdb, ctx +func GetRedisClient() rueidis.Client { + return rdb } diff --git a/worker/internal/tasks/file.go b/worker/internal/tasks/file.go index 5952e86..7e40f2c 100644 --- a/worker/internal/tasks/file.go +++ b/worker/internal/tasks/file.go @@ -34,13 +34,13 @@ func RemoveFile(ctx context.Context, task *asynq.Task) error { } } - rdb, rctx := u.GetRedisClient() + rdb := u.GetRedisClient() uploadPath, err := u.GetUploadDirPath() if err != nil { return err } filePath := filepath.Join(uploadPath, payload.FileId) - if err := rdb.Do(rctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil { + if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil { return err } if err := os.RemoveAll(filePath); err != nil { diff --git a/worker/internal/tasks/share.go b/worker/internal/tasks/share.go index 110d4bd..0a45b93 100644 --- a/worker/internal/tasks/share.go +++ b/worker/internal/tasks/share.go @@ -34,7 +34,7 @@ func RemoveShare(ctx context.Context, task *asynq.Task) error { return x != payload.ShareId }) if len(shareIDs) == 0 { - rdb, ctx := u.GetRedisClient() + rdb := u.GetRedisClient() uploadPath, err := u.GetUploadDirPath() if err != nil { return err diff --git a/worker/main.go b/worker/main.go index 85cd3a5..a31ede5 100644 --- a/worker/main.go +++ b/worker/main.go @@ -23,6 +23,11 @@ func main() { } defer logger.Sync() //nolint:errcheck zap.ReplaceGlobals(logger) + // redis + if err := utils.InitRedis(); err != nil { + logger.Fatal("redis init failed", zap.Error(err)) + panic(err) + } if err := i18n.Init(); err != nil { log.Fatalf("failed to init i18n: %v", err)