Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"cache.go",
"conf.go",
"dao.go",
"rediskey.go",
],
importpath = "go-common/app/job/live/xroom-feed/internal/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/live/xroom-feed/internal/model:go_default_library",
"//app/service/live/dao-anchor/api/grpc/v1: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"],
)

View File

@@ -0,0 +1,45 @@
package dao
import (
"context"
daoAnchorV1 "go-common/app/service/live/dao-anchor/api/grpc/v1"
"go-common/library/log"
)
func (d *Dao) SetRecPoolCache(ctx context.Context, key string, list string, expire int) (err error) {
log.Info("[SetRecPoolCache]key: %s", key)
rconn := d.redis.Get(ctx)
defer rconn.Close()
if _, err = rconn.Do("SET", key, list, "EX", expire); err != nil {
log.Error("[SetRecPoolCache]redis.Set error(%v)", err)
}
return
}
func (d *Dao) SetRecInfoCache(ctx context.Context, list map[int64]*daoAnchorV1.RoomData) (err error) {
rconn := d.redis.Get(ctx)
defer rconn.Close()
//rconn.Send("MULTI")
for roomId, roomData := range list {
if roomData == nil {
continue
}
key, expire := d.getRecInfoKey(roomId)
rconn.Send("HMSET", key, "room_id", roomData.RoomId, "uid", roomData.Uid, "title", roomData.Title, "popularity_count", roomData.PopularityCount, "Keyframe", roomData.Keyframe, "cover", roomData.Cover, "parent_area_id", roomData.ParentAreaId, "parent_area_name", roomData.ParentAreaName, "area_id", roomData.AreaId, "area_name", roomData.AreaName)
rconn.Send("EXPIRE", key, expire)
}
if err = rconn.Flush(); err != nil {
log.Error("[SetRecInfoCache]redis.Set error(%v)", err)
}
for _, roomData := range list {
if roomData == nil {
continue
}
rconn.Receive()
rconn.Receive()
}
return
}

View File

@@ -0,0 +1,69 @@
package dao
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"go-common/app/job/live/xroom-feed/internal/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
_sqlConf = "select id, name, type, rules, module_type, position, percent, priority from ap_rec_pool_conf where is_del = 0 and (status = 2 or (status = 1 and start_time < '%s' and end_time > '%s'))"
_sqlWhiteList = "select room_id from ap_rec_white_list where rec_id = %d and is_del = 0"
)
func (d *Dao) GetConfFromDb() (ruleConf []*model.RecPoolConf, err error) {
var rows *sql.Rows
ruleConf = make([]*model.RecPoolConf, 0)
timeNow := time.Now().Format("2006-01-02 15:04:05")
rows, err = d.liveAppDb.Query(context.TODO(), fmt.Sprintf(_sqlConf, timeNow, timeNow))
if err != nil {
log.Error("getConfFromDb_error:%+v", err)
return
}
for rows.Next() {
r := new(model.RecPoolConf)
if err = rows.Scan(&r.Id, &r.Name, &r.ConfType, &r.Rules, &r.ModuleType, &r.Position, &r.Percent, &r.Priority); err != nil {
log.Error("getConfFromDb_parseError:%+v", err)
continue
}
ruleConf = append(ruleConf, r)
}
return
}
func (d *Dao) GetWhiteList(ctx context.Context, id int) (whiteStr string, err error) {
//白名单获取
var whiteRows *sql.Rows
whiteRows, err = d.liveAppDb.Query(ctx, fmt.Sprintf(_sqlWhiteList, id))
if err != nil {
log.Error("getWhiteListFromDb_error:%+v", err)
return
}
whiteList := make([]string, 0)
for whiteRows.Next() {
r := new(model.RecWhiteList)
if err = whiteRows.Scan(&r.RoomId); err != nil {
log.Error("getWhiteListFromDb_parseError:%+v", err)
continue
}
if r.RoomId == 0 {
continue
}
whiteList = append(whiteList, strconv.Itoa(r.RoomId))
}
if len(whiteList) <= 0 {
return
}
whiteStr = strings.Join(whiteList, ",")
return
}

View File

@@ -0,0 +1,78 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/library/cache/redis"
"go-common/library/conf/paladin"
"go-common/library/database/sql"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_recInfoExpireTtl = 86400
_recInfoKey = "rec_info_%d"
)
// Dao dao.
type Dao struct {
liveAppDb *sql.DB
redis *redis.Pool
redisExpire int32
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
// New new a dao and return.
func New() (dao *Dao) {
var (
dc struct {
LiveApp *sql.Config
}
rc struct {
Rec *redis.Config
DemoExpire xtime.Duration
}
)
checkErr(paladin.Get("mysql.toml").UnmarshalTOML(&dc))
checkErr(paladin.Get("redis.toml").UnmarshalTOML(&rc))
fmt.Printf("%+v", dc)
dao = &Dao{
// mysql
liveAppDb: sql.NewMySQL(dc.LiveApp),
// redis
redis: redis.NewPool(rc.Rec),
redisExpire: int32(time.Duration(rc.DemoExpire) / time.Second),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.redis.Close()
d.liveAppDb.Close()
}
// Ping ping the resource.
func (d *Dao) Ping(ctx context.Context) (err error) {
if err = d.pingRedis(ctx); err != nil {
return
}
return d.liveAppDb.Ping(ctx)
}
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
}

View File

@@ -0,0 +1,9 @@
package dao
import "fmt"
func (d *Dao) getRecInfoKey(roomId int64) (key string, expire int){
key = fmt.Sprintf(_recInfoKey, roomId)
expire = _recInfoExpireTtl
return
}