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,59 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"relation.go",
],
importpath = "go-common/app/job/main/relation-cache/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/relation-cache/conf:go_default_library",
"//app/service/main/relation/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"relation_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/relation-cache/conf:go_default_library",
"//app/service/main/relation/model:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,59 @@
package dao
import (
"context"
"time"
"go-common/app/job/main/relation-cache/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
xtime "go-common/library/time"
)
// Dao dao
type Dao struct {
*cacheTTL
c *conf.Config
mc *memcache.Pool
redis *redis.Pool
db *xsql.DB
client *bm.Client
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
cacheTTL: &cacheTTL{
RelationTTL: asSecond(c.CacheTTL.RelationTTL),
},
c: c,
mc: memcache.NewPool(c.Memcache),
redis: redis.NewPool(c.Redis),
db: xsql.NewMySQL(c.MySQL),
client: bm.NewClient(c.HTTPClient),
}
return
}
type cacheTTL struct {
RelationTTL int64
}
func asSecond(d xtime.Duration) int64 {
return int64(time.Duration(d) / time.Second)
}
// Close close the resource.
func (d *Dao) Close() {
d.mc.Close()
d.redis.Close()
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
// TODO: if you need use mc,redis, please add
return d.db.Ping(c)
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"flag"
"go-common/app/job/main/relation-cache/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.relation-cache-job")
flag.Set("conf_token", "db8cc8578e4447edfe1e7385c62be0e5")
flag.Set("tree_id", "56981")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,164 @@
package dao
import (
"context"
"fmt"
"strconv"
relation "go-common/app/service/main/relation/model"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_prefixFollowings = "at_"
_prefixTags = "tags_" // user tag info.
_prefixFollowing = "pb_a_"
_prefixStat = "c_" // key of stat
_prefixTagCount = "rs_tmtc_%d" // key of relation tag by mid & tag's count
)
func statKey(mid int64) string {
return _prefixStat + strconv.FormatInt(mid, 10)
}
func tagsKey(mid int64) string {
return _prefixTags + strconv.FormatInt(mid, 10)
}
func followingsKey(mid int64) string {
return _prefixFollowings + strconv.FormatInt(mid, 10)
}
func followingKey(mid int64) string {
return _prefixFollowing + strconv.FormatInt(mid, 10)
}
func tagCountKey(mid int64) string {
return fmt.Sprintf(_prefixTagCount, mid)
}
// DelStatCache is
func (d *Dao) DelStatCache(ctx context.Context, mid int64) error {
conn := d.mc.Get(ctx)
defer conn.Close()
if err := conn.Delete(statKey(mid)); err != nil {
if err == memcache.ErrNotFound {
return nil
}
log.Error("Failed to delete stat cache: mid: %d: %+v", mid, err)
return err
}
return nil
}
// DelFollowerCache del follower cache
func (d *Dao) DelFollowerCache(ctx context.Context, fid int64) error {
key := followingKey(fid)
conn := d.mc.Get(ctx)
defer conn.Close()
if err := conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
return err
}
}
return nil
}
// DelFollowing del following cache.
func (d *Dao) DelFollowing(c context.Context, mid int64, following *relation.Following) (err error) {
var (
ok bool
key = followingsKey(mid)
)
conn := d.redis.Get(c)
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.RelationTTL)); err != nil {
log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
} else if ok {
if _, err = conn.Do("HDEL", key, following.Mid); err != nil {
log.Error("conn.Do(HDEL, %s, %d) error(%v)", key, following.Mid, err)
}
}
conn.Close()
return
}
// DelTagsCache is
func (d *Dao) DelTagsCache(ctx context.Context, mid int64) (err error) {
conn := d.mc.Get(ctx)
if err = conn.Delete(tagsKey(mid)); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
}
}
conn.Close()
return
}
// AddFollowingCache is
func (d *Dao) AddFollowingCache(c context.Context, mid int64, following *relation.Following) (err error) {
var (
ok bool
key = followingsKey(mid)
)
conn := d.redis.Get(c)
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.RelationTTL)); err != nil {
log.Error("redis.Bool(conn.Do(EXPIRE, %s)) error(%v)", key, err)
} else if ok {
var ef []byte
if ef, err = d.encode(following.Attribute, following.MTime, following.Tag, following.Special); err != nil {
return
}
if _, err = conn.Do("HSET", key, following.Mid, ef); err != nil {
log.Error("conn.Do(HSET, %s, %d) error(%v)", key, following.Mid, err)
}
}
conn.Close()
return
}
// encode
func (d *Dao) encode(attribute uint32, mtime xtime.Time, tagids []int64, special int32) (res []byte, err error) {
ft := &relation.FollowingTags{Attr: attribute, Ts: mtime, TagIds: tagids, Special: special}
return ft.Marshal()
}
// DelFollowingCache delete following cache.
func (d *Dao) DelFollowingCache(c context.Context, mid int64) (err error) {
return d.delFollowingCache(c, followingKey(mid))
}
// delFollowingCache delete following cache.
func (d *Dao) delFollowingCache(c context.Context, key string) (err error) {
conn := d.mc.Get(c)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", key, err)
}
}
conn.Close()
return
}
// DelTagCountCache del tag count cache.
func (d *Dao) DelTagCountCache(c context.Context, mid int64) (err error) {
conn := d.mc.Get(c)
if err = conn.Delete(tagCountKey(mid)); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
log.Error("conn.Delete(%s) error(%v)", tagCountKey(mid), err)
}
}
conn.Close()
return
}

View File

@@ -0,0 +1,192 @@
package dao
import (
"context"
relation "go-common/app/service/main/relation/model"
xtime "go-common/library/time"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaostatKey(t *testing.T) {
var (
mid = int64(1)
)
convey.Convey("statKey", t, func(ctx convey.C) {
p1 := statKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaotagsKey(t *testing.T) {
var (
mid = int64(1)
)
convey.Convey("tagsKey", t, func(ctx convey.C) {
p1 := tagsKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaofollowingsKey(t *testing.T) {
var (
mid = int64(1)
)
convey.Convey("followingsKey", t, func(ctx convey.C) {
p1 := followingsKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaofollowingKey(t *testing.T) {
var (
mid = int64(1)
)
convey.Convey("followingKey", t, func(ctx convey.C) {
p1 := followingKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaotagCountKey(t *testing.T) {
var (
mid = int64(1)
)
convey.Convey("tagCountKey", t, func(ctx convey.C) {
p1 := tagCountKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelStatCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
)
convey.Convey("DelStatCache", t, func(ctx convey.C) {
err := d.DelStatCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelFollowerCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
)
convey.Convey("DelFollowerCache", t, func(ctx convey.C) {
err := d.DelFollowerCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelFollowing(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
following = &relation.Following{}
)
convey.Convey("DelFollowing", t, func(ctx convey.C) {
err := d.DelFollowing(c, mid, following)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelTagsCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
)
convey.Convey("DelTagsCache", t, func(ctx convey.C) {
err := d.DelTagsCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoAddFollowingCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
following = &relation.Following{}
)
convey.Convey("AddFollowingCache", t, func(ctx convey.C) {
err := d.AddFollowingCache(c, mid, following)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoencode(t *testing.T) {
var (
attribute = uint32(1)
mtime xtime.Time
tagids = []int64{1}
special = int32(1)
)
convey.Convey("encode", t, func(ctx convey.C) {
res, err := d.encode(attribute, mtime, tagids, special)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelFollowingCache(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
)
convey.Convey("DelFollowingCache", t, func(ctx convey.C) {
err := d.DelFollowingCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaodelFollowingCache(t *testing.T) {
var (
c = context.Background()
key = ""
)
convey.Convey("delFollowingCache", t, func(ctx convey.C) {
err := d.delFollowingCache(c, key)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelTagCountCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DelTagCountCache", t, func(ctx convey.C) {
err := d.DelTagCountCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}