mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor: update Redis operations to use rueidis client for improved performance across multiple models
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user