90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package vidhelp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"91porn-server/common/constant/redisconst"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models/v/vidmod"
|
|
"91porn-server/web/webg"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func getVidKey(vids []primitive.ObjectID) []string {
|
|
format := redisconst.VideoInfoKey()
|
|
keys := make([]string, len(vids))
|
|
for i, v := range vids {
|
|
keys[i] = fmt.Sprintf(format, v.Hex())
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func parseVideoInfo(data []interface{}) []*vidmod.VideoModel {
|
|
infos := make([]*vidmod.VideoModel, 0, len(data))
|
|
for _, d := range data {
|
|
str, ok := d.(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
var v vidmod.VideoModel
|
|
if err := json.Unmarshal([]byte(str), &v); err != nil {
|
|
continue
|
|
}
|
|
infos = append(infos, &v)
|
|
}
|
|
return infos
|
|
}
|
|
|
|
func getVideoListByIDsFromRedis(ids []primitive.ObjectID) ([]*vidmod.VideoModel, error) {
|
|
sInfo := getVidKey(ids)
|
|
uInfo, err := webg.Redis.MGet(sInfo...)
|
|
if err != nil {
|
|
log.Error("getVideoListByIDsFromRedis error", log.Any("ids", ids), log.Any("sInfo", sInfo), log.E(err))
|
|
return nil, err
|
|
}
|
|
videoInfo := parseVideoInfo(uInfo)
|
|
return videoInfo, nil
|
|
}
|
|
|
|
func setVideoListByIDs2Redis(infos []*vidmod.VideoModel) error {
|
|
expire := redisconst.VideoInfoExpire()
|
|
format := redisconst.VideoInfoKey()
|
|
for _, v := range infos {
|
|
d, err := json.Marshal(v)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
s := fmt.Sprintf(format, v.ID.Hex())
|
|
_ = webg.Redis.Set(s, string(d), expire)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func incComment(vid primitive.ObjectID) {
|
|
infos, err := getVideoListByIDsFromRedis([]primitive.ObjectID{vid})
|
|
if err != nil || len(infos) == 0 {
|
|
return
|
|
}
|
|
if infos[0] == nil {
|
|
return
|
|
}
|
|
infos[0].CommentCount++
|
|
infos[0].FakeCommentCount++
|
|
_ = setVideoListByIDs2Redis(infos)
|
|
}
|
|
|
|
func decComment(vid primitive.ObjectID) {
|
|
infos, err := getVideoListByIDsFromRedis([]primitive.ObjectID{vid})
|
|
if err != nil || len(infos) == 0 {
|
|
return
|
|
}
|
|
if infos[0] == nil {
|
|
return
|
|
}
|
|
infos[0].CommentCount--
|
|
infos[0].FakeCommentCount--
|
|
_ = setVideoListByIDs2Redis(infos)
|
|
}
|