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

56
app/admin/main/up/service/cache/BUILD vendored Normal file
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 = [
"cache.go",
"tag.go",
"video_types.go",
],
importpath = "go-common/app/admin/main/up/service/cache",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/up/conf:go_default_library",
"//app/interface/main/creative/model/tag: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"],
tags = ["automanaged"],
deps = [
"//app/admin/main/up/conf:go_default_library",
"//app/interface/main/creative/model/tag: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,45 @@
package cache
import (
"testing"
"flag"
"github.com/smartystreets/goconvey/convey"
"go-common/app/admin/main/up/conf"
"os"
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.up-admin")
flag.Set("conf_token", "930697bb7def4df0713ef8080596b863")
flag.Set("tree_id", "36438")
flag.Set("conf_version", "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/up-admin.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/up-admin.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) {
})
})
})
}

45
app/admin/main/up/service/cache/tag.go vendored Normal file
View File

@@ -0,0 +1,45 @@
package cache
import (
"go-common/app/interface/main/creative/model/tag"
"sync"
"time"
)
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,50 @@
package cache
import (
"go-common/app/interface/main/creative/model/tag"
"testing"
"time"
"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(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
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,50 @@
package cache
import (
"context"
"go-common/app/admin/main/up/conf"
"go-common/app/service/main/videoup/model/archive"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"time"
)
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.Normal)
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(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 (
tids = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tpNames := GetTidName(tids)
ctx.Convey("Then tpNames should not be nil.", func(ctx convey.C) {
ctx.So(tpNames, convey.ShouldNotBeNil)
})
})
})
}