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_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"memcache_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-player/model/archive:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
"redis.go",
],
importpath = "go-common/app/job/main/app-player/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/model/archive:go_default_library",
"//app/job/main/app-player/conf:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis: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"],
)

View File

@@ -0,0 +1,37 @@
package dao
import (
"context"
"go-common/app/job/main/app-player/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
)
// Dao is dao.
type Dao struct {
// mc
mc *memcache.Pool
// redis
redis *redis.Pool
}
// New new a dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// mc
mc: memcache.NewPool(c.Memcache),
// reids
redis: redis.NewPool(c.Redis),
}
return
}
// PingMc is
func (d *Dao) PingMc(c context.Context) (err error) {
conn := d.mc.Get(c)
item := &memcache.Item{Key: "ping", Value: []byte{1}, Flags: memcache.FlagRAW, Expiration: 0}
err = conn.Set(item)
conn.Close()
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestPingMc(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.PingMc(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,31 @@
package dao
import (
"context"
"strconv"
"go-common/app/interface/main/app-player/model/archive"
"go-common/library/cache/memcache"
"github.com/pkg/errors"
)
const (
_prefixArc = "p_"
)
func keyArc(aid int64) string {
return _prefixArc + strconv.FormatInt(aid, 10)
}
// AddArchiveCache add archive cache.
func (d *Dao) AddArchiveCache(c context.Context, aid int64, arc *archive.Info) (err error) {
conn := d.mc.Get(c)
key := keyArc(aid)
item := &memcache.Item{Key: key, Object: arc, Flags: memcache.FlagProtobuf, Expiration: 0}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "conn.Set(%v)", item)
}
conn.Close()
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/app-player/model/archive"
"github.com/smartystreets/goconvey/convey"
)
func TestAddArchiveCache(t *testing.T) {
var (
c = context.Background()
aid = int64(1)
arc = &archive.Info{Aid: 1}
)
convey.Convey("AddArchiveCache", t, func(ctx convey.C) {
err := d.AddArchiveCache(c, aid, arc)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,55 @@
package dao
import (
"context"
"encoding/json"
"go-common/library/cache/redis"
"github.com/pkg/errors"
)
const (
_failList = "player_job_list"
)
func keyRetry() string {
return _failList
}
// PushList rpush item to redis
func (d *Dao) PushList(c context.Context, a interface{}) (err error) {
var bs []byte
conn := d.redis.Get(c)
defer conn.Close()
if bs, err = json.Marshal(a); err != nil {
err = errors.Wrapf(err, "%v", a)
return
}
if _, err = conn.Do("RPUSH", keyRetry(), bs); err != nil {
err = errors.Wrapf(err, "conn.Do(RPUSH,%s,%s)", keyRetry(), bs)
}
return
}
// PopList lpop item from redis
func (d *Dao) PopList(c context.Context) (bs []byte, err error) {
conn := d.redis.Get(c)
if bs, err = redis.Bytes(conn.Do("LPOP", keyRetry())); err != nil {
if err == redis.ErrNil {
err = nil
} else {
err = errors.Wrapf(err, "redis.Bytes(conn.Do(LPOP, %s))", keyRetry())
}
}
conn.Close()
return
}
// PingRedis is
func (d *Dao) PingRedis(c context.Context) (err error) {
var conn = d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestPushList(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("PushList", t, func(ctx convey.C) {
err := d.PushList(c, nil)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestPopList(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("PopList", t, func(ctx convey.C) {
_, err := d.PopList(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestPingRedis(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("PingRedis", t, func(ctx convey.C) {
err := d.PingRedis(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}