mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(redis): initialize Redis client and update context handling in Redis operations
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user