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,73 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
"memcache_test.go",
"stat_test.go",
"view_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-view/conf:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//library/net/rpc:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"bnj.go",
"dao.go",
"memcache.go",
"stat.go",
"view.go",
],
importpath = "go-common/app/interface/main/app-view/dao/archive",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-view/conf:go_default_library",
"//app/interface/main/app-view/model/view:go_default_library",
"//app/interface/main/history/model:go_default_library",
"//app/interface/main/history/rpc/client:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/api/gorpc:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/sync/errgroup:go_default_library",
"//library/xstr: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,140 @@
package archive
import (
"context"
"net/http"
"net/url"
"strconv"
"go-common/app/interface/main/app-view/model/view"
"go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_realteURL = "/recsys/related"
_commercialURL = "/x/internal/creative/arc/commercial"
_relateRecURL = "/recommand"
_playURL = "/playurl/batch"
)
// RelateAids get relate by aid
func (d *Dao) RelateAids(c context.Context, aid int64) (aids []int64, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("key", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Data []*struct {
Value string `json:"value"`
} `json:"data"`
}
if err = d.client.Get(c, d.realteURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.realteURL+"?"+params.Encode())
return
}
if len(res.Data) != 0 {
if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
err = errors.Wrap(err, res.Data[0].Value)
}
}
return
}
// Commercial is
func (d *Dao) Commercial(c context.Context, aid int64) (gameID int64, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("aid", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Data *struct {
GameID int64 `json:"game_id"`
} `json:"data"`
}
if err = d.client.Get(c, d.commercialURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.commercialURL+"?"+params.Encode())
return
}
if res.Data != nil {
gameID = res.Data.GameID
}
return
}
// NewRelateAids relate online recommend 在线实时推荐
func (d *Dao) NewRelateAids(c context.Context, aid, mid int64, build, parentMode int, buvid, from string, plat int8) (rec []*view.NewRelateRec, userFeature, returnCode string, dalaoExp int, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("from", "2")
params.Set("cmd", "related")
params.Set("timeout", "100")
params.Set("plat", strconv.Itoa(int(plat)))
params.Set("build", strconv.Itoa(build))
params.Set("buvid", buvid)
params.Set("from_av", strconv.FormatInt(aid, 10))
params.Set("request_cnt", "40")
params.Set("source_page", from)
params.Set("parent_mode", strconv.Itoa(parentMode))
params.Set("need_dalao", "1")
var res struct {
Code int `json:"code"`
Data []*view.NewRelateRec `json:"data"`
UserFeature string `json:"user_feature"`
DalaoExp int `json:"dalao_exp"`
}
log.Warn("dalaotest url(%s)", d.relateRecURL+"?"+params.Encode())
if err = d.client.Get(c, d.relateRecURL, ip, params, &res); err != nil {
returnCode = "500"
return
}
returnCode = strconv.Itoa(res.Code)
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.relateRecURL+"?"+params.Encode())
return
}
dalaoExp = res.DalaoExp
userFeature = res.UserFeature
rec = res.Data
return
}
// PlayerInfos cid with player info
func (d *Dao) PlayerInfos(c context.Context, cids []int64, qn, fnver, fnval, forceHost int, platform string) (pm map[uint32]*archive.BvcVideoItem, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("cid", xstr.JoinInts(cids))
params.Set("qn", strconv.Itoa(qn))
params.Set("platform", platform)
params.Set("uip", ip)
params.Set("layout", "pb")
params.Set("fnver", strconv.Itoa(fnver))
params.Set("fnval", strconv.Itoa(fnval))
params.Set("force_host", strconv.Itoa(forceHost))
var req *http.Request
if req, err = d.client.NewRequest("GET", d.playURL, ip, params); err != nil {
return
}
res := new(archive.BvcResponseMsg)
if err = d.httpClient.PB(c, req, res); err != nil {
return
}
if int(res.Code) != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(int(res.Code)), d.playURL+params.Encode())
return
}
pm = res.Data
return
}

View File

@@ -0,0 +1,21 @@
package archive
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestRelateAids(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
)
convey.Convey("Ping", t, func(ctx convey.C) {
_, err := d.RelateAids(c, aid)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,19 @@
package archive
import (
"context"
"go-common/app/service/main/archive/model/archive"
)
// ViewsRPC is
func (d *Dao) ViewsRPC(c context.Context, aids []int64) (avm map[int64]*archive.View3, err error) {
avm, err = d.arcRPC.Views3(c, &archive.ArgAids2{Aids: aids})
return
}
// ViewRPC is
func (d *Dao) ViewRPC(c context.Context, aid int64) (v *archive.View3, err error) {
v, err = d.arcRPC.View3(c, &archive.ArgAid2{Aid: aid})
return
}

View File

@@ -0,0 +1,220 @@
package archive
import (
"context"
"runtime"
"time"
"go-common/app/interface/main/app-view/conf"
"go-common/app/interface/main/app-view/model/view"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/sync/errgroup"
history "go-common/app/interface/main/history/model"
hisrpc "go-common/app/interface/main/history/rpc/client"
arcrpc "go-common/app/service/main/archive/api/gorpc"
"go-common/library/cache/memcache"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
// Dao is archive dao.
type Dao struct {
// http client
client *bm.Client
httpClient *bm.Client
realteURL string
commercialURL string
relateRecURL string
playURL string
// rpc
arcRPC *arcrpc.Service2
hisRPC *hisrpc.Service
// memcache
arcMc *memcache.Pool
expireArc int32
expireRlt int32
// chan
mCh chan func()
}
// New new a archive dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// http client
client: bm.NewClient(c.HTTPWrite),
httpClient: bm.NewClient(c.HTTPClient),
arcRPC: arcrpc.New2(c.ArchiveRPC),
hisRPC: hisrpc.New(c.HisRPC),
realteURL: c.Host.Data + _realteURL,
commercialURL: c.Host.APICo + _commercialURL,
relateRecURL: c.Host.Data + _relateRecURL,
playURL: c.Host.Bvcvod + _playURL,
// memcache
arcMc: memcache.NewPool(c.Memcache.Archive.Config),
expireArc: int32(time.Duration(c.Memcache.Archive.ArchiveExpire) / time.Second),
expireRlt: int32(time.Duration(c.Memcache.Archive.RelateExpire) / time.Second),
// mc proc
mCh: make(chan func(), 10240),
}
// video db
for i := 0; i < runtime.NumCPU()*2; i++ {
go d.cacheproc()
}
return
}
// Ping ping check memcache connection
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingMC(c)
}
// Archive3 get archive.
func (d *Dao) Archive3(c context.Context, aid int64) (a *api.Arc, err error) {
arg := &archive.ArgAid2{Aid: aid}
if a, err = d.arcRPC.Archive3(c, arg); err != nil {
log.Error("d.arcRPC.Archive3(%v) error(%+v)", arg, err)
return
}
return
}
// Archives multi get archives.
func (d *Dao) Archives(c context.Context, aids []int64) (as map[int64]*api.Arc, err error) {
if len(aids) == 0 {
return
}
var stm map[int64]*api.Stat
g, ctx := errgroup.WithContext(c)
g.Go(func() (err error) {
var missed []int64
if as, missed, err = d.arcsCache(ctx, aids); err != nil {
as = make(map[int64]*api.Arc, len(aids))
missed = aids
log.Error("%+v", err)
err = nil
}
if len(missed) == 0 {
return
}
var tmp map[int64]*api.Arc
arg := &archive.ArgAids2{Aids: missed}
if tmp, err = d.arcRPC.Archives3(ctx, arg); err != nil {
log.Error("d.arcRPC.Archives3(%v) error(%v)", arg, err)
return
}
for aid, a := range tmp {
as[aid] = a
}
return
})
g.Go(func() (err error) {
var missed []int64
if stm, missed, err = d.statsCache(ctx, aids); err != nil {
stm = make(map[int64]*api.Stat, len(aids))
missed = aids
log.Error("%+v", err)
err = nil
}
if len(missed) == 0 {
return
}
var tmp map[int64]*api.Stat
arg := &archive.ArgAids2{Aids: missed}
if tmp, err = d.arcRPC.Stats3(ctx, arg); err != nil {
log.Error("d.arcRPC.Stats3(%v) error(%v)", arg, err)
err = nil
return
}
for aid, st := range tmp {
stm[aid] = st
}
return
})
if err = g.Wait(); err != nil {
log.Error("%+v", err)
return
}
for aid, a := range as {
if st, ok := stm[aid]; ok {
a.Stat = *st
}
}
return
}
// Shot get video shot.
func (d *Dao) Shot(c context.Context, aid, cid int64) (shot *archive.Videoshot, err error) {
ip := metadata.String(c, metadata.RemoteIP)
arg := &archive.ArgCid2{Aid: aid, Cid: cid, RealIP: ip}
return d.arcRPC.Videoshot2(c, arg)
}
// UpCount2 get upper count.
func (d *Dao) UpCount2(c context.Context, mid int64) (cnt int, err error) {
arg := &archive.ArgUpCount2{Mid: mid}
if cnt, err = d.arcRPC.UpCount2(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}
// UpArcs3 get upper archives.
func (d *Dao) UpArcs3(c context.Context, mid int64, pn, ps int) (as []*api.Arc, err error) {
arg := &archive.ArgUpArcs2{Mid: mid, Pn: pn, Ps: ps}
if as, err = d.arcRPC.UpArcs3(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}
// Progress is archive plays progress .
func (d *Dao) Progress(c context.Context, aid, mid int64) (h *view.History, err error) {
ip := metadata.String(c, metadata.RemoteIP)
arg := &history.ArgPro{Mid: mid, Aids: []int64{aid}, RealIP: ip}
his, err := d.hisRPC.Progress(c, arg)
if err != nil {
log.Error("d.hisRPC.Progress(%v) error(%v)", arg, err)
return
}
if his[aid] != nil {
h = &view.History{Cid: his[aid].Cid, Progress: his[aid].Pro}
}
return
}
// Archive 用的时候注意了这个方法得到的稿件Stat不是最新的
func (d *Dao) Archive(c context.Context, aid int64) (a *api.Arc, err error) {
if a, err = d.arcCache(c, aid); err != nil {
log.Error("%+v", err)
} else if a != nil {
return
}
if a, err = d.arcRPC.Archive3(c, &archive.ArgAid2{Aid: aid}); err != nil {
log.Error("d.arcRPC.Archive3(%d) error(%v)", aid, err)
return
}
return
}
// addCache add archive to mc or redis
func (d *Dao) addCache(f func()) {
select {
case d.mCh <- f:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc write memcache and stat redis use goroutine
func (d *Dao) cacheproc() {
for {
f := <-d.mCh
f()
}
}

View File

@@ -0,0 +1,162 @@
package archive
import (
"context"
"flag"
"os"
"testing"
"time"
"go-common/app/interface/main/app-view/conf"
"go-common/app/service/main/archive/model/archive"
"go-common/library/net/rpc"
xtime "go-common/library/time"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-view")
flag.Set("conf_token", "3a4CNLBhdFbRQPs7B4QftGvXHtJo92xw")
flag.Set("tree_id", "4575")
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)
}
func TestViewRPC(t *testing.T) {
convey.Convey("TestViewRPC", t, func(ctx convey.C) {
var addr = "172.22.36.185:6089" // new
// addr = "172.22.38.5:6089" // old
client := rpc.Dial(addr, xtime.Duration(100*time.Millisecond), nil)
var (
view *archive.View3
views map[int64]*archive.View3
err error
)
if err = client.Call(context.TODO(), "RPC.View3", &archive.ArgAid{Aid: 10111165}, &view); err != nil {
ctx.Println(err)
return
}
ctx.Println(view)
if err = client.Call(context.TODO(), "RPC.Views3", &archive.ArgAids{Aids: []int64{10111165}}, &views); err != nil {
ctx.Println(err)
return
}
ctx.Println(views)
})
}
func TestPing(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("Ping", t, func(ctx convey.C) {
d.Ping(c)
})
}
func TestArchive3(t *testing.T) {
var (
c = context.TODO()
aid = int64(0)
)
convey.Convey("Archive3", t, func(ctx convey.C) {
_, err := d.Archive3(c, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestArchives(t *testing.T) {
var (
c = context.TODO()
aids = []int64{1}
)
convey.Convey("Archives", t, func(ctx convey.C) {
_, err := d.Archives(c, aids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestShot(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
cid = int64(2)
)
convey.Convey("Shot", t, func(ctx convey.C) {
d.Shot(c, aid, cid)
})
}
func TestUpCount2(t *testing.T) {
var (
c = context.TODO()
mid = int64(1)
)
convey.Convey("UpCount2", t, func(ctx convey.C) {
_, err := d.UpCount2(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestUpArcs3(t *testing.T) {
var (
c = context.TODO()
mid = int64(1)
pn = int(1)
ps = int(20)
)
convey.Convey("UpArcs3", t, func(ctx convey.C) {
d.UpArcs3(c, mid, pn, ps)
})
}
func TestProgress(t *testing.T) {
var (
c = context.TODO()
aid = int64(1)
mid = int64(1)
)
convey.Convey("Progress", t, func(ctx convey.C) {
_, err := d.Progress(c, aid, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestArchive(t *testing.T) {
var (
c = context.TODO()
aid = int64(-1)
)
convey.Convey("Archive", t, func(ctx convey.C) {
_, err := d.Archive(c, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,246 @@
package archive
import (
"context"
"strconv"
"go-common/app/interface/main/app-view/model/view"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_prefixRelate = "al_"
_prefixViewStatic = "avp_"
_prefixStat = "stp_"
_prefixArchive = "a3p_"
_prefixViewContribute = "avpc_"
)
func keyRl(aid int64) string {
return _prefixRelate + strconv.FormatInt(aid, 10)
}
func keyView(aid int64) string {
return _prefixViewStatic + strconv.FormatInt(aid, 10)
}
func keyStat(aid int64) string {
return _prefixStat + strconv.FormatInt(aid, 10)
}
func keyArc(aid int64) string {
return _prefixArchive + strconv.FormatInt(aid, 10)
}
func keyViewContribute(mid int64) string {
return _prefixViewContribute + strconv.FormatInt(mid, 10)
}
// AddRelatesCache add relates
func (d *Dao) AddRelatesCache(aid int64, rls []*view.Relate) {
d.addCache(func() {
d.addRelatesCache(context.TODO(), aid, rls)
})
}
// RelatesCache get relates.
func (d *Dao) RelatesCache(c context.Context, aid int64) (rls []*view.Relate, err error) {
conn := d.arcMc.Get(c)
key := keyRl(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
if err = conn.Scan(r, &rls); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// addRelatesCache add relates cache.
func (d *Dao) addRelatesCache(c context.Context, aid int64, rls []*view.Relate) (err error) {
conn := d.arcMc.Get(c)
key := keyRl(aid)
item := &memcache.Item{Key: key, Object: rls, Flags: memcache.FlagJSON, Expiration: d.expireRlt}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%v,%d)", key, rls, d.expireRlt)
}
conn.Close()
return
}
// viewCache get view cache from remote memecache .
func (d *Dao) viewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
conn := d.arcMc.Get(c)
key := keyView(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
vs = &archive.View3{Archive3: &archive.Archive3{}}
if err = conn.Scan(r, vs); err != nil {
vs = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// statCache get a archive stat from cache.
func (d *Dao) statCache(c context.Context, aid int64) (st *api.Stat, err error) {
conn := d.arcMc.Get(c)
key := keyStat(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
st = &api.Stat{}
if err = conn.Scan(r, st); err != nil {
st = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// statsCache get stat cache by aids
func (d *Dao) statsCache(c context.Context, aids []int64) (cached map[int64]*api.Stat, missed []int64, err error) {
cached = make(map[int64]*api.Stat, len(aids))
conn := d.arcMc.Get(c)
defer conn.Close()
keys := make([]string, 0, len(aids))
for _, aid := range aids {
keys = append(keys, keyStat(aid))
}
rs, err := conn.GetMulti(keys)
if err != nil {
err = errors.Wrapf(err, "conn.GetMulti(%v)", keys)
return
}
for _, item := range rs {
var st = &api.Stat{}
if err = conn.Scan(item, st); err != nil {
err = nil
log.Error("conn.Scan(%v) error(%v)", item, err)
continue
}
cached[st.Aid] = st
}
if len(cached) == len(aids) {
return
}
for _, aid := range aids {
if _, ok := cached[aid]; !ok {
missed = append(missed, aid)
}
}
return
}
// arcsCache get archives cache.
func (d *Dao) arcsCache(c context.Context, aids []int64) (cached map[int64]*api.Arc, missed []int64, err error) {
cached = make(map[int64]*api.Arc, len(aids))
conn := d.arcMc.Get(c)
defer conn.Close()
keys := make([]string, 0, len(aids))
aidmap := make(map[string]int64, len(aids))
for _, aid := range aids {
k := keyArc(aid)
if _, ok := aidmap[k]; !ok {
keys = append(keys, k)
aidmap[k] = aid
}
}
rs, err := conn.GetMulti(keys)
if err != nil {
err = errors.Wrapf(err, "conn.GetMulti(%v)", keys)
return
}
for k, r := range rs {
a := &api.Arc{}
if err = conn.Scan(r, a); err != nil {
log.Error("conn.Scan(%s) error(%v)", r.Value, err)
err = nil
continue
}
cached[aidmap[k]] = a
// delete hit key
delete(aidmap, k)
}
// missed key
missed = make([]int64, 0, len(aidmap))
for _, aid := range aidmap {
missed = append(missed, aid)
}
return
}
// arcCache get archive cache.
func (d *Dao) arcCache(c context.Context, aid int64) (a *api.Arc, err error) {
conn := d.arcMc.Get(c)
key := keyArc(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
a = &api.Arc{}
if err = conn.Scan(r, a); err != nil {
a = nil
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// ViewContributeCache get archive cache.
func (d *Dao) ViewContributeCache(c context.Context, mid int64) (aids []int64, err error) {
conn := d.arcMc.Get(c)
key := keyViewContribute(mid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
if err = conn.Scan(r, &aids); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.arcMc.Get(c)
err = conn.Set(&memcache.Item{Key: "ping", Object: []byte{1}, Flags: memcache.FlagJSON, Expiration: 0})
conn.Close()
return
}

View File

@@ -0,0 +1,26 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AddRelatesCache(t *testing.T) {
Convey("AddRelatesCache", t, func() {
d.AddRelatesCache(1, nil)
})
}
func Test_RelatesCache(t *testing.T) {
Convey("RelatesCache", t, func() {
d.RelatesCache(context.TODO(), 1)
})
}
func Test_ViewContributeCache(t *testing.T) {
Convey("ViewContributeCache", t, func() {
d.ViewContributeCache(context.TODO(), 2)
})
}

View File

@@ -0,0 +1,23 @@
package archive
import (
"context"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/log"
)
// Stat get a archive stat.
func (d *Dao) Stat(c context.Context, aid int64) (st *api.Stat, err error) {
if st, err = d.statCache(c, aid); err != nil {
log.Error("%+v", err)
} else if st != nil {
return
}
arg := &archive.ArgAid2{Aid: aid}
if st, err = d.arcRPC.Stat3(c, arg); err != nil {
log.Error("d.arcRPC.Stat3(%v) error(%v)", arg, err)
}
return
}

View File

@@ -0,0 +1,14 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDao_Stat(t *testing.T) {
Convey("Stat", t, func() {
d.Stat(context.TODO(), 2)
})
}

View File

@@ -0,0 +1,68 @@
package archive
import (
"context"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
// View3 view archive with pages pb.
func (d *Dao) View3(c context.Context, aid int64) (v *archive.View3, err error) {
arg := &archive.ArgAid2{Aid: aid}
if v, err = d.arcRPC.View3(c, arg); err != nil {
log.Error("d.arcRPC.View3(%v) error(%+v)", arg, err)
if ecode.Cause(err) == ecode.NothingFound {
err = nil
return
}
}
return
}
// ViewCache get view static data from cache if cache missed from rpc.
func (d *Dao) ViewCache(c context.Context, aid int64) (vs *archive.View3, err error) {
if aid == 0 {
return
}
if vs, err = d.viewCache(c, aid); err != nil {
return
}
if vs != nil && vs.Archive3 != nil && len(vs.Pages) != 0 {
var st *api.Stat
if st, err = d.Stat(c, aid); err != nil {
log.Error("%+v", err)
err = nil
return
}
if st != nil {
vs.Archive3.Stat = archive.Stat3{
Aid: st.Aid,
View: st.View,
Danmaku: st.Danmaku,
Reply: st.Reply,
Fav: st.Fav,
Coin: st.Coin,
Share: st.Share,
NowRank: st.NowRank,
HisRank: st.HisRank,
Like: st.Like,
DisLike: st.DisLike,
}
}
}
return
}
// Description get archive description by aid.
func (d *Dao) Description(c context.Context, aid int64) (desc string, err error) {
arg := &archive.ArgAid{Aid: aid}
if desc, err = d.arcRPC.Description2(c, arg); err != nil {
err = errors.Wrapf(err, "%v", arg)
}
return
}

View File

@@ -0,0 +1,26 @@
package archive
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_View3(t *testing.T) {
Convey("View3", t, func() {
d.View3(context.TODO(), 1)
})
}
func Test_ViewCache(t *testing.T) {
Convey("ViewCache", t, func() {
d.ViewCache(context.TODO(), 1)
})
}
func Test_Description(t *testing.T) {
Convey("Description", t, func() {
d.Description(context.TODO(), 2)
})
}