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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"block.go",
"dao.go",
"member.go",
],
importpath = "go-common/app/job/main/member-cache/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/member-cache/conf:go_default_library",
"//app/job/main/member-cache/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/pkg/errors: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 = [
"block_test.go",
"dao_test.go",
"member_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/member-cache/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"fmt"
"go-common/library/cache/memcache"
"github.com/pkg/errors"
)
func userKey(mid int64) (key string) {
key = fmt.Sprintf("u_%d", mid)
return
}
// DeleteUserBlockCache is.
func (d *Dao) DeleteUserBlockCache(c context.Context, mid int64) (err error) {
var (
key = userKey(mid)
conn = d.blockMemcache.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
return nil
}
err = errors.WithStack(err)
return
}
return
}

View File

@@ -0,0 +1,33 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaouserKey(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("userKey", t, func(ctx convey.C) {
key := userKey(mid)
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
}
func TestDaoDeleteUserBlockCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DeleteUserBlockCache", t, func(ctx convey.C) {
err := d.DeleteUserBlockCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"go-common/app/job/main/member-cache/conf"
"go-common/library/cache/memcache"
)
// Dao dao
type Dao struct {
c *conf.Config
memberMemcache *memcache.Pool
blockMemcache *memcache.Pool
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
memberMemcache: memcache.NewPool(c.MemberMemcache),
blockMemcache: memcache.NewPool(c.BlockMemcache),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.memberMemcache.Close()
d.blockMemcache.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
return nil
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/job/main/member-cache/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.member-cache-job")
flag.Set("conf_token", "31291cb6cd6b252ff27bf58d7fec72a6")
flag.Set("tree_id", "56587")
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,135 @@
package dao
import (
"context"
"fmt"
"strconv"
"go-common/app/job/main/member-cache/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_expPrefix = "exp_%d"
_expExpire = 86400
)
func expKey(mid int64) string {
return fmt.Sprintf(_expPrefix, mid)
}
func keyInfo(mid int64) string {
return model.CacheKeyInfo + strconv.FormatInt(mid, 10)
}
func (d *Dao) mcBaseKey(mid int64) (key string) {
return fmt.Sprintf(model.CacheKeyBase, mid)
}
func (d *Dao) moralKey(mid int64) (key string) {
return fmt.Sprintf(model.CacheKeyMoral, mid)
}
func realnameInfoKey(mid int64) string {
return fmt.Sprintf("realname_info_%d", mid)
}
func realnameApplyStatusKey(mid int64) string {
return fmt.Sprintf("realname_apply_%d", mid)
}
// DelMoralCache delete moral cache.
func (d *Dao) DelMoralCache(c context.Context, mid int64) (err error) {
key := d.moralKey(mid)
conn := d.memberMemcache.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}
// DelBaseInfoCache delete baseInfo cache.
func (d *Dao) DelBaseInfoCache(c context.Context, mid int64) (err error) {
key := d.mcBaseKey(mid)
conn := d.memberMemcache.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}
// DelInfoCache delete account info from cache.
func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
conn := d.memberMemcache.Get(c)
err = conn.Delete(keyInfo(mid))
conn.Close()
if err == memcache.ErrNotFound {
err = nil
}
return
}
// SetExpCache set user exp cache.
func (d *Dao) SetExpCache(c context.Context, mid, exp int64) (err error) {
conn := d.memberMemcache.Get(c)
defer conn.Close()
if err = conn.Set(&memcache.Item{
Key: expKey(mid),
Value: []byte(strconv.FormatInt(exp, 10)),
Expiration: _expExpire,
}); err != nil {
log.Error("setexpcache mid %d err %v ", mid, err)
}
return
}
// DelExpCache set user exp cache.
func (d *Dao) DelExpCache(c context.Context, mid int64) error {
conn := d.memberMemcache.Get(c)
defer conn.Close()
if err := conn.Delete(expKey(mid)); err != nil {
if err == memcache.ErrNotFound {
return nil
}
return errors.WithStack(err)
}
return nil
}
// DeleteRealnameCache delete all realname cache
func (d *Dao) DeleteRealnameCache(c context.Context, mid int64) (err error) {
var (
key1 = realnameInfoKey(mid)
key2 = realnameApplyStatusKey(mid)
conn = d.memberMemcache.Get(c)
)
defer conn.Close()
if err = conn.Delete(key1); err != nil {
if err != memcache.ErrNotFound {
err = errors.Wrapf(err, "conn.Delete(%s)", key1)
return
}
err = nil
}
if err = conn.Delete(key2); err != nil {
if err == memcache.ErrNotFound {
err = nil
} else {
err = errors.Wrapf(err, "conn.Delete(%s)", key2)
}
return
}
return
}

View File

@@ -0,0 +1,147 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoexpKey(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("expKey", t, func(ctx convey.C) {
p1 := expKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaokeyInfo(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("keyInfo", t, func(ctx convey.C) {
p1 := keyInfo(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaomcBaseKey(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("mcBaseKey", t, func(ctx convey.C) {
key := d.mcBaseKey(mid)
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
}
func TestDaomoralKey(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("moralKey", t, func(ctx convey.C) {
key := d.moralKey(mid)
ctx.Convey("Then key should not be nil.", func(ctx convey.C) {
ctx.So(key, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelMoralCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DelMoralCache", t, func(ctx convey.C) {
err := d.DelMoralCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelBaseInfoCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DelBaseInfoCache", t, func(ctx convey.C) {
err := d.DelBaseInfoCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelInfoCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DelInfoCache", t, func(ctx convey.C) {
err := d.DelInfoCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaorealnameApplyStatusKey(t *testing.T) {
var (
mid = int64(0)
)
convey.Convey("realnameApplyStatusKey", t, func(ctx convey.C) {
p1 := realnameApplyStatusKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaoDeleteRealnameCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DeleteRealnameCache", t, func(ctx convey.C) {
err := d.DeleteRealnameCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoSetExpCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
exp = int64(0)
)
convey.Convey("SetExpCache", t, func(ctx convey.C) {
err := d.SetExpCache(c, mid, exp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoDelExpCache(t *testing.T) {
var (
c = context.Background()
mid = int64(0)
)
convey.Convey("DelExpCache", t, func(ctx convey.C) {
err := d.DelExpCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}