@@ -0,0 +1,65 @@
|
||||
package vidpopmod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/log"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func InsertOne(cfg VideoPopularityConfig) error {
|
||||
if _, err := coll(nil).UpdateMany(bson.M{"isActive": true}, bson.M{"$set": bson.M{"isActive": false}}); err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.CreatedAt = time.Now()
|
||||
cfg.IsActive = true
|
||||
_, err := coll(nil).InsertOne(cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func FindOne() (VideoPopularityConfig, error) {
|
||||
cfg := VideoPopularityConfig{}
|
||||
return cfg, coll(nil).FindOne(&cfg, bson.M{"isActive": true})
|
||||
}
|
||||
|
||||
func FindVidPopCfg() (VideoPopularityConfig, error) {
|
||||
cfg := VideoPopularityConfig{}
|
||||
//先从redis拿数据
|
||||
redisKey := table
|
||||
str, err := appg.Redis.Get(redisKey)
|
||||
if err != nil { // redis 错误不向上报告
|
||||
log.Error("FindVidPopCfg redisc.Get", log.Any("redisKey", redisKey), log.E(err))
|
||||
}
|
||||
if str != nil {
|
||||
err = json.Unmarshal([]byte(*str), &cfg)
|
||||
if err == nil {
|
||||
return cfg, nil
|
||||
} else { // json.Unmarshal的错误不向上报告,而是尝试去DB获取用户
|
||||
log.Error("FindVidPopCfg json.Unmarshal", log.Any("redisKey", redisKey), log.E(err))
|
||||
}
|
||||
}
|
||||
//没有数据, 查数据库
|
||||
if err = coll(nil).FindOne(&cfg, bson.M{"isActive": true}); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
if cfg.ID.IsZero() {
|
||||
return cfg, nil
|
||||
}
|
||||
//结果存redis
|
||||
common.Go(func() {
|
||||
jsonBytes, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
log.Error("FindVidPopCfg json.Marshal", log.E(err))
|
||||
return
|
||||
}
|
||||
if err := appg.Redis.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire); err != nil {
|
||||
log.Error("FindVidPopCfg redisc.Set", log.Any("redisKey", redisKey), log.E(err))
|
||||
}
|
||||
})
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user