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 = [
"mc_wallet_test.go",
"wallet_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/live/wallet/conf:go_default_library",
"//app/job/live/wallet/model:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mc_wallet.go",
"wallet.go",
],
importpath = "go-common/app/job/live/wallet/dao",
tags = ["automanaged"],
deps = [
"//app/job/live/wallet/conf:go_default_library",
"//app/job/live/wallet/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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,55 @@
package dao
import (
"context"
"go-common/app/job/live/wallet/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
type Dao struct {
c *conf.Config
db *xsql.DB
mc *memcache.Pool
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.DB.Wallet),
mc: memcache.NewPool(c.Memcache.Wallet),
}
return
}
// Close close the resource.
func (dao *Dao) Close() {
dao.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("PingDb error(%v)", err)
return
}
return d.pingMC(c)
}
// pingMc ping
func (d *Dao) pingMC(c context.Context) (err error) {
item := &memcache.Item{
Key: "ping",
Value: []byte{1},
}
conn := d.mc.Get(c)
err = conn.Set(item)
conn.Close()
if err != nil {
log.Error("PingMemcache conn.Set(%v) error(%v)", item, err)
}
return
}

View File

@@ -0,0 +1,30 @@
package dao
import (
"context"
"fmt"
mc "go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_walletMcKey = "wu:%d"
)
func mcKey(uid int64) string {
return fmt.Sprintf(_walletMcKey, uid)
}
// DelWalletCache 删除等级缓存
func (d *Dao) DelWalletCache(c context.Context, uid int64) (err error) {
key := mcKey(uid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err == mc.ErrNotFound {
err = nil
} else if err != nil {
log.Error("[dao.mc_wallet|DelWalletCache] conn.Delete(%s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,14 @@
package dao
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestDelWalletCache(t *testing.T) {
Convey("Test Del Memcache", t, func() {
once.Do(startService)
err := d.DelWalletCache(ctx, 10000)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"fmt"
"go-common/app/job/live/wallet/model"
"go-common/library/log"
)
const (
_shard = 10
_insWallet = "INSERT IGNORE INTO user_wallet_%d(uid,gold,iap_gold,silver) VALUES(?,?,?,?)"
_mergeWallet = "INSERT INTO user_wallet_%d(uid,gold,iap_gold,silver) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE gold=gold+%d,iap_gold=iap_gold+%d,silver=silver+%d"
)
func tableIndex(uid int64) int64 {
return uid % _shard
}
func (d *Dao) InitWallet(c context.Context, user *model.User) (row int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insWallet, tableIndex(user.Uid)), user.Uid, user.Gold, user.IapGold, user.Silver)
if err != nil {
log.Error("[dao.wallet|InitWallet] d.db.Exec err: %v {uid:%d gold:%d iap_gold:%d silver:%d}", err, user.Uid, user.Gold, user.IapGold, user.Silver)
return
}
return res.RowsAffected()
}
func (d *Dao) MergeWallet(c context.Context, uid int64, gold int64, iap_gold int64, silver int64) (row int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_mergeWallet, tableIndex(uid), gold, iap_gold, silver), uid, gold, iap_gold, silver)
if err != nil {
log.Error("[dao.wallet|MergeWallet] d.db.Exec err: %v {uid:%d gold:%d iap_gold:%d silver:%d}", err, uid, gold, iap_gold, silver)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,53 @@
package dao
import (
"context"
"sync"
"testing"
"time"
"go-common/app/job/live/wallet/conf"
"go-common/library/log"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/job/live/wallet/model"
)
var (
once sync.Once
d *Dao
ctx = context.TODO()
testUid int64
)
func initConf() {
if err := conf.Init(); err != nil {
panic(err)
}
log.Init(conf.Conf.Log)
defer log.Close()
}
func startService() {
initConf()
d = New(conf.Conf)
time.Sleep(time.Second * 2)
}
func TestInitWallet(t *testing.T) {
Convey("Init Wallet", t, func() {
once.Do(startService)
user := &model.User{}
user.Uid = testUid
_, err := d.InitWallet(ctx, user)
So(err, ShouldBeNil)
})
}
func TestMergeWallet(t *testing.T) {
Convey("Merge Wallet", t, func() {
once.Do(startService)
_, err := d.MergeWallet(ctx, testUid, 1, 2, 3)
So(err, ShouldBeNil)
})
}