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,51 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mc_level.go",
],
importpath = "go-common/app/job/live-userexp/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/live-userexp/conf:go_default_library",
"//app/job/live-userexp/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["mc_level_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/live-userexp/conf:go_default_library",
"//app/job/live-userexp/model:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey: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,69 @@
package dao
import (
"context"
"go-common/app/job/live-userexp/conf"
"go-common/library/cache/memcache"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao struct userexp-service dao
type Dao struct {
c *conf.Config
// exp db
expDb *sql.DB
// memcache
expMc *memcache.Pool
cacheExpire int32
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
expDb: sql.NewMySQL(c.DB.Exp),
expMc: memcache.NewPool(c.Memcache.Exp),
cacheExpire: c.LevelExpire,
}
return
}
// Ping check service health.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.expDb.Ping(c); err != nil {
log.Error("PingDb error(%v)", err)
return
}
if err = d.pingMemcache(c); err != nil {
return
}
return
}
// PingMemcache check connection success.
func (d *Dao) pingMemcache(c context.Context) (err error) {
item := &memcache.Item{
Key: "ping",
Value: []byte{1},
Expiration: d.cacheExpire,
}
conn := d.expMc.Get(c)
err = conn.Set(item)
conn.Close()
if err != nil {
log.Error("PingMemcache conn.Set(%v) error(%v)", item, err)
}
return
}
// Close close memcache resource.
func (d *Dao) Close() {
if d.expMc != nil {
d.expMc.Close()
}
if d.expDb != nil {
d.expDb.Close()
}
}

View File

@@ -0,0 +1,35 @@
package dao
import (
"context"
"fmt"
"go-common/app/job/live-userexp/model"
mc "go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_expKey = "level:%d"
)
func key(uid int64) string {
return fmt.Sprintf(_expKey, uid)
}
// SetLevelCache 设置等级缓存
func (d *Dao) SetLevelCache(c context.Context, level *model.Level) (err error) {
key := key(level.Uid)
conn := d.expMc.Get(c)
defer conn.Close()
if conn.Set(&mc.Item{
Key: key,
Object: level,
Flags: mc.FlagProtobuf,
Expiration: d.cacheExpire,
}); err != nil {
log.Error("[dao.mc_exp|SetLevelCache] conn.Set(%s, %v) error(%v)", key, level, err)
}
return
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"sync"
"testing"
"time"
"go-common/app/job/live-userexp/conf"
"go-common/app/job/live-userexp/model"
"go-common/library/log"
. "github.com/smartystreets/goconvey/convey"
)
var (
once sync.Once
d *Dao
ctx = context.TODO()
)
func initConf() {
if err := conf.Init(); err != nil {
panic(err)
}
log.Init(conf.Conf.Log)
defer log.Close()
}
func startService() {
initConf()
d = New(conf.Conf)
time.Sleep(time.Second * 2)
}
func TestSetLevelCache(t *testing.T) {
Convey("SetLevelCache", t, func() {
once.Do(startService)
err := d.SetLevelCache(ctx, &model.Level{Uid: 10001, Uexp: 1000, Rexp: 100, Ulevel: 2, Rlevel: 1, Color: 12345})
So(err, ShouldBeNil)
})
}