@@ -0,0 +1,42 @@
|
||||
package vidpopmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const table = models.VideoPopularityConfig
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
type VideoPopularityConfig struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // ID
|
||||
InitialPopularity int `json:"initalPop" bson:"initalPop"` // 初始热度值
|
||||
PlayTimePercentage []PlayTimePercentage `json:"playTimePercentage" bson:"playTimePercentage"` // 会员视频有效播放量的百分比
|
||||
ReviewTimePower float64 `json:"reviewTimePower" bson:"reviewTimePower"` // 过审时长幂
|
||||
EffectivePlayCountMultiplier int `json:"effectivePlayCountMultiplier" bson:"effectivePlayCountMultiplier"` // 有效播放量放大因子
|
||||
LikeCountMultiplier int `json:"LikeCountMultiplier" bson:"LikeCountMultiplier"` // 点赞量放大因子
|
||||
IsActive bool `json:"isActive" bson:"isActive"` // 是否开启IsActive
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
type PlayTimePercentage struct {
|
||||
Min int `json:"min" bson:"min"` // 视频时长下限
|
||||
Max int `json:"max" bson:"max"` // 视频时长上限
|
||||
Percentage int `json:"percentage" bson:"percentage"` // 有效播放时长比例
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
}
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
@@ -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