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

57
app/interface/main/mcn/dao/cache/BUILD vendored Normal file
View File

@@ -0,0 +1,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"cache.go",
"tag.go",
"video_types.go",
],
importpath = "go-common/app/interface/main/mcn/dao/cache",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/model/tag:go_default_library",
"//app/interface/main/mcn/conf:go_default_library",
"//app/service/main/videoup/model/archive:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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 = [
"cache_test.go",
"tag_test.go",
"video_types_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/model/tag:go_default_library",
"//app/interface/main/mcn/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,9 @@
package cache
import "time"
//LoadCache load cache
func LoadCache() {
var now = time.Now()
RefreshUpType(now)
}

View File

@@ -0,0 +1,46 @@
package cache
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/mcn/conf"
"github.com/smartystreets/goconvey/convey"
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.mcn-interface")
flag.Set("conf_token", "49e4671bafbf93059aeb602685052ca0")
flag.Set("tree_id", "58909")
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")
} else {
flag.Set("conf", "../../cmd/mcn-interface.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/mcn-interface.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
os.Exit(m.Run())
}
func TestCacheLoadCache(t *testing.T) {
convey.Convey("LoadCache", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
LoadCache()
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}

46
app/interface/main/mcn/dao/cache/tag.go vendored Normal file
View File

@@ -0,0 +1,46 @@
package cache
import (
"sync"
"time"
"go-common/app/interface/main/creative/model/tag"
)
var (
//TagCache tag's cache key=>tag_id, value=>*tag.Meta
TagCache = make(map[int64]*tag.Meta)
tagMutex sync.Mutex
)
//ClearTagCache clear all tag cache
func ClearTagCache(tm time.Time) {
TagCache = make(map[int64]*tag.Meta)
}
//AddTagCache add tag cache
func AddTagCache(meta *tag.Meta) {
if meta == nil {
return
}
tagMutex.Lock()
TagCache[meta.TagID] = meta
tagMutex.Unlock()
}
//GetTagCache get tag cache
func GetTagCache(ids ...int64) (result map[int64]*tag.Meta, leftIDs []int64) {
result = make(map[int64]*tag.Meta)
tagMutex.Lock()
for _, v := range ids {
var d, ok = TagCache[v]
if !ok {
leftIDs = append(leftIDs, v)
continue
}
result[v] = d
}
tagMutex.Unlock()
return
}

View File

@@ -0,0 +1,52 @@
package cache
import (
"testing"
"time"
"go-common/app/interface/main/creative/model/tag"
"github.com/smartystreets/goconvey/convey"
)
func TestCacheClearTagCache(t *testing.T) {
convey.Convey("ClearTagCache", t, func(ctx convey.C) {
var (
tm = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ClearTagCache(tm)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}
func TestCacheAddTagCache(t *testing.T) {
convey.Convey("AddTagCache", t, func(ctx convey.C) {
var (
meta = &tag.Meta{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
AddTagCache(meta)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}
func TestCacheGetTagCache(t *testing.T) {
convey.Convey("GetTagCache", t, func(ctx convey.C) {
var (
ids = int64(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
LoadCache()
result, leftIDs := GetTagCache(ids)
ctx.Convey("Then result,leftIDs should not be nil.", func(ctx convey.C) {
ctx.So(leftIDs, convey.ShouldNotBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,60 @@
package cache
import (
"context"
"time"
"go-common/app/interface/main/mcn/conf"
"go-common/app/service/main/videoup/model/archive"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
var (
//VideoUpTypeCache cache for video types, map key is tid
VideoUpTypeCache = make(map[int]archive.Type)
)
//RefreshUpTypeAsync refresh in a goroutine
func RefreshUpTypeAsync(tm time.Time) {
go RefreshUpType(tm)
}
//RefreshUpType refresh
func RefreshUpType(tm time.Time) {
var url = conf.Conf.Host.Videoup + "/videoup/types"
var client = bm.NewClient(conf.Conf.HTTPClient)
var result struct {
Code int `json:"code"`
Data map[int]archive.Type `json:"data"`
}
var err = client.Get(context.Background(), url, "", nil, &result)
if err != nil {
log.Error("refresh videoup types fail, err=%v", err)
return
}
log.Info("refresh videoup types ok")
VideoUpTypeCache = result.Data
}
//GetTidName get tid name
func GetTidName(tid int64) string {
info, ok := VideoUpTypeCache[int(tid)]
if !ok {
return ""
}
return info.Name
}
// GetTidNames get tid name
func GetTidNames(tids []int64) (tpNames map[int64]string) {
tpNames = make(map[int64]string, len(tids))
for _, tid := range tids {
info, ok := VideoUpTypeCache[int(tid)]
if !ok {
continue
}
tpNames[tid] = info.Name
}
return
}

View File

@@ -0,0 +1,48 @@
package cache
import (
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestCacheRefreshUpTypeAsync(t *testing.T) {
convey.Convey("RefreshUpTypeAsync", t, func(ctx convey.C) {
var (
tm = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
RefreshUpTypeAsync(tm)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}
func TestCacheRefreshUpType(t *testing.T) {
convey.Convey("RefreshUpType", t, func(ctx convey.C) {
var (
tm = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
RefreshUpType(tm)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}
func TestCacheGetTidName(t *testing.T) {
convey.Convey("GetTidName", t, func(ctx convey.C) {
var (
tid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := GetTidName(tid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}