mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor: streamline Redis stat updates by using a handler function to modify stat data directly
This commit is contained in:
@@ -125,17 +125,10 @@ func VaildateShare(c *echo.Context) error {
|
||||
|
||||
// 统计分享数
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
statData, _ := models.GetRedisStat(currentDate)
|
||||
if statData == nil {
|
||||
statData = &models.StatData{
|
||||
FileSize: 0,
|
||||
FileNum: 0,
|
||||
ShareNum: 0,
|
||||
DownloadNum: 0,
|
||||
}
|
||||
}
|
||||
statData.DownloadNum += 1
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, func(stat *models.StatData) *models.StatData {
|
||||
stat.DownloadNum += 1
|
||||
return stat
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
@@ -241,18 +241,11 @@ func FinishUploadTask(c *echo.Context) error {
|
||||
}
|
||||
// 统计
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
statData, _ := models.GetRedisStat(currentDate)
|
||||
if statData == nil {
|
||||
statData = &models.StatData{
|
||||
FileSize: 0,
|
||||
FileNum: 0,
|
||||
ShareNum: 0,
|
||||
DownloadNum: 0,
|
||||
}
|
||||
}
|
||||
statData.FileSize += fileInfo.FileSize
|
||||
statData.FileNum += 1
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, func(stat *models.StatData) *models.StatData {
|
||||
stat.FileSize += fileInfo.FileSize
|
||||
stat.FileNum += 1
|
||||
return stat
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
@@ -128,17 +128,10 @@ func CreateShareInfo(c *echo.Context) error {
|
||||
|
||||
// 统计分享数
|
||||
currentDate := time.Now().Format("2006-01-02")
|
||||
statData, _ := models.GetRedisStat(currentDate)
|
||||
if statData == nil {
|
||||
statData = &models.StatData{
|
||||
FileSize: 0,
|
||||
FileNum: 0,
|
||||
ShareNum: 0,
|
||||
DownloadNum: 0,
|
||||
}
|
||||
}
|
||||
statData.ShareNum += 1
|
||||
err = models.SetRedisStat(currentDate, *statData)
|
||||
err = models.SetRedisStat(currentDate, func(stat *models.StatData) *models.StatData {
|
||||
stat.ShareNum += 1
|
||||
return stat
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
|
||||
"pkg/utils"
|
||||
|
||||
"dario.cat/mergo"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -33,15 +32,21 @@ func GetRedisStat(key string) (*StatData, error) {
|
||||
return &stat, nil
|
||||
}
|
||||
|
||||
func SetRedisStat(key string, stat StatData) error {
|
||||
func SetRedisStat(key string, handler func(stat *StatData) *StatData) error {
|
||||
rdb, ctx := utils.GetRedisClient()
|
||||
old_stat, err := GetRedisStat(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if old_stat != nil {
|
||||
mergo.Merge(&stat, old_stat)
|
||||
if old_stat == nil {
|
||||
old_stat = &StatData{
|
||||
FileSize: 0,
|
||||
FileNum: 0,
|
||||
ShareNum: 0,
|
||||
DownloadNum: 0,
|
||||
}
|
||||
}
|
||||
stat := handler(old_stat)
|
||||
jsonData, _ := json.Marshal(stat)
|
||||
_, err = rdb.HSet(ctx, "015:stat", key, string(jsonData)).Result()
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user