Create & Init Project...
This commit is contained in:
40
app/service/live/xroom-feed/internal/dao/BUILD
Normal file
40
app/service/live/xroom-feed/internal/dao/BUILD
Normal file
@ -0,0 +1,40 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"dao.go",
|
||||
"recdata.go",
|
||||
],
|
||||
importpath = "go-common/app/service/live/xroom-feed/internal/dao",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/service/live/xroom-feed/internal/model:go_default_library",
|
||||
"//library/cache/memcache:go_default_library",
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/conf/paladin:go_default_library",
|
||||
"//library/database/sql:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/time:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
96
app/service/live/xroom-feed/internal/dao/dao.go
Normal file
96
app/service/live/xroom-feed/internal/dao/dao.go
Normal file
@ -0,0 +1,96 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go-common/library/cache/memcache"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/conf/paladin"
|
||||
"go-common/library/database/sql"
|
||||
"go-common/library/log"
|
||||
xtime "go-common/library/time"
|
||||
)
|
||||
|
||||
// Dao dao.
|
||||
type Dao struct {
|
||||
db *sql.DB
|
||||
redis *redis.Pool
|
||||
redisExpire int32
|
||||
mc *memcache.Pool
|
||||
mcExpire int32
|
||||
}
|
||||
|
||||
func checkErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// New new a dao and return.
|
||||
func New() (dao *Dao) {
|
||||
var (
|
||||
// dc struct {
|
||||
// Demo *sql.Config
|
||||
// }
|
||||
rc struct {
|
||||
Rec *redis.Config
|
||||
RecExpire xtime.Duration
|
||||
}
|
||||
// mc struct {
|
||||
// Demo *memcache.Config
|
||||
// DemoExpire xtime.Duration
|
||||
// }
|
||||
)
|
||||
// checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
|
||||
checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
|
||||
// checkErr(paladin.Get("memcache.toml").UnmarshalTOML(&mc))
|
||||
dao = &Dao{
|
||||
// mysql
|
||||
// db: sql.NewMySQL(dc.Demo),
|
||||
// redis
|
||||
redis: redis.NewPool(rc.Rec),
|
||||
redisExpire: int32(time.Duration(rc.RecExpire) / time.Second),
|
||||
// memcache
|
||||
// mc: memcache.NewPool(mc.Demo),
|
||||
// mcExpire: int32(time.Duration(mc.DemoExpire) / time.Second),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Close close the resource.
|
||||
func (d *Dao) Close() {
|
||||
// d.mc.Close()
|
||||
d.redis.Close()
|
||||
// d.db.Close()
|
||||
}
|
||||
|
||||
// Ping ping the resource.
|
||||
func (d *Dao) Ping(ctx context.Context) (err error) {
|
||||
// if err = d.pingMC(ctx); err != nil {
|
||||
// return
|
||||
// }
|
||||
if err = d.pingRedis(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
// return d.db.Ping(ctx)
|
||||
}
|
||||
|
||||
// func (d *Dao) pingMC(ctx context.Context) (err error) {
|
||||
// conn := d.mc.Get(ctx)
|
||||
// defer conn.Close()
|
||||
// if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
|
||||
// log.Error("conn.Set(PING) error(%v)", err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func (d *Dao) pingRedis(ctx context.Context) (err error) {
|
||||
conn := d.redis.Get(ctx)
|
||||
defer conn.Close()
|
||||
if _, err = conn.Do("SET", "ping", "pong"); err != nil {
|
||||
log.Error("conn.Set(PING) error(%v)", err)
|
||||
}
|
||||
return
|
||||
}
|
120
app/service/live/xroom-feed/internal/dao/recdata.go
Normal file
120
app/service/live/xroom-feed/internal/dao/recdata.go
Normal file
@ -0,0 +1,120 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go-common/app/service/live/xroom-feed/internal/model"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
var (
|
||||
recConfKey = "rec_conf"
|
||||
recPoolKey = "rec_pool_%d"
|
||||
recRoomInfo = "rec_info_%d"
|
||||
)
|
||||
|
||||
//GetCacheData 获取缓存数据
|
||||
func (d *Dao) GetCacheData() (data []byte) {
|
||||
conn := d.redis.Get(context.Background())
|
||||
defer conn.Close()
|
||||
|
||||
var err error
|
||||
if data, err = redis.Bytes(conn.Do("GET", recConfKey)); err != nil {
|
||||
log.Error("[recdata cache] GetCacheData err:%+v", err)
|
||||
return make([]byte, 0)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
//GetRecInfoByRoomid 批量通过Roomid获取rec_info
|
||||
func (d *Dao) GetRecInfoByRoomid(ctx context.Context, roomids []int64) map[int64]*model.RecRoomInfo {
|
||||
conn := d.redis.Get(ctx)
|
||||
defer conn.Close()
|
||||
|
||||
redisSuccess := make([]int64, 0, len(roomids))
|
||||
for _, roomid := range roomids {
|
||||
key := fmt.Sprintf(recRoomInfo, roomid)
|
||||
if err := conn.Send("HGETALL", key); err != nil {
|
||||
log.Error("[recdata] GetRecInfoByRoomid redis send roomid:%d err:%v", roomid, err)
|
||||
continue
|
||||
}
|
||||
redisSuccess = append(redisSuccess, roomid)
|
||||
}
|
||||
|
||||
conn.Flush()
|
||||
|
||||
result := make(map[int64]*model.RecRoomInfo)
|
||||
for _, roomid := range redisSuccess {
|
||||
var roominfo map[string]string
|
||||
var err error
|
||||
if roominfo, err = redis.StringMap(conn.Receive()); err != nil {
|
||||
log.Error("[recdata] GetRecInfoByRoomid redis receive err:%d err:%+v", roomid, err)
|
||||
continue
|
||||
}
|
||||
if len(roominfo) <= 0 {
|
||||
log.Error("[recdata] GetRecInfoByRoomid redis receive empty:%d err:+%v", roomid, roominfo)
|
||||
continue
|
||||
}
|
||||
|
||||
result[roomid] = model.NewRecRoomInfo()
|
||||
result[roomid].Title = roominfo["title"]
|
||||
result[roomid].Uid, _ = strconv.ParseInt(roominfo["uid"], 10, 64)
|
||||
result[roomid].PopularityCount, _ = strconv.ParseInt(roominfo["popularity_count"], 10, 64)
|
||||
result[roomid].KeyFrame = roominfo["Keyframe"]
|
||||
result[roomid].Cover = roominfo["cover"]
|
||||
result[roomid].ParentAreaID, _ = strconv.ParseInt(roominfo["parent_area_id"], 10, 64)
|
||||
result[roomid].ParentAreaName = roominfo["parent_area_name"]
|
||||
result[roomid].AreaID, _ = strconv.ParseInt(roominfo["area_id"], 10, 64)
|
||||
result[roomid].AreaName = roominfo["area_name"]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
//GetRecPoolByID 通过id获取推荐池
|
||||
func (d *Dao) GetRecPoolByID(ctx context.Context, rids []int64) map[int64][]int64 {
|
||||
conn := d.redis.Get(ctx)
|
||||
defer conn.Close()
|
||||
|
||||
var (
|
||||
room string
|
||||
err error
|
||||
)
|
||||
|
||||
redisSuccess := make([]int64, 0, len(rids))
|
||||
for _, rid := range rids {
|
||||
key := fmt.Sprintf(recPoolKey, rid)
|
||||
if err := conn.Send("GET", key); err != nil {
|
||||
log.Error("[recdata] GetRecPoolByID send id %d err:%+v", rid, err)
|
||||
continue
|
||||
}
|
||||
redisSuccess = append(redisSuccess, rid)
|
||||
}
|
||||
|
||||
conn.Flush()
|
||||
|
||||
result := make(map[int64][]int64)
|
||||
for _, rid := range redisSuccess {
|
||||
if room, err = redis.String(conn.Receive()); err != nil {
|
||||
log.Error("[recdata] GetRecPoolByID receive id %d err:%+v", rid, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if room == "" {
|
||||
log.Info("[recdata] GetRecPoolByID receive empty room list rid: %d", rid)
|
||||
continue
|
||||
}
|
||||
rooms := strings.Split(room, ",")
|
||||
roomIDs := make([]int64, len(rooms), len(rooms))
|
||||
|
||||
for i, roomid := range rooms {
|
||||
roomIDs[i], _ = strconv.ParseInt(roomid, 10, 64)
|
||||
}
|
||||
result[rid] = roomIDs
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user