model创建
This commit is contained in:
parent
c7517d345a
commit
69cfaf0737
34
modules/dbmodel.go
Normal file
34
modules/dbmodel.go
Normal file
@ -0,0 +1,34 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/sys/cache"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type DBModel struct {
|
||||
model proto.Message
|
||||
}
|
||||
|
||||
func (m *DBModel) Model(model proto.Message) *DBModel {
|
||||
m.model = model
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DBModel) OnChange() {
|
||||
|
||||
}
|
||||
|
||||
func (m *DBModel) Set() {
|
||||
|
||||
}
|
||||
|
||||
//获取数据
|
||||
func (m *DBModel) Get(key string) {
|
||||
err := cache.GetRds().Get(key, m.model)
|
||||
if err != nil {
|
||||
fmt.Printf("err:%v", err)
|
||||
return
|
||||
}
|
||||
}
|
32
modules/dbmodel_test.go
Normal file
32
modules/dbmodel_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_dreamfactory/pb"
|
||||
"go_dreamfactory/sys/cache"
|
||||
"go_dreamfactory/sys/db"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if err := db.OnInit(nil, db.Set_MongodbUrl("mongodb://admin:123456@10.0.0.9:27018"), db.Set_MongodbDatabase("dreamfactory")); err != nil {
|
||||
fmt.Printf("err:%v\n", err)
|
||||
return
|
||||
}
|
||||
if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
|
||||
fmt.Printf("err:%v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer os.Exit(m.Run())
|
||||
|
||||
}
|
||||
|
||||
func TestInitData(t *testing.T) {
|
||||
dbmodel := new(DBModel)
|
||||
model := &pb.Cache_UserData{}
|
||||
dbmodel.Model(model)
|
||||
dbmodel.Get("user:62a2bd064098aa8ff982f2e2")
|
||||
fmt.Printf("%v", model.SessionId)
|
||||
}
|
8
modules/user/model_user.go
Normal file
8
modules/user/model_user.go
Normal file
@ -0,0 +1,8 @@
|
||||
package user
|
||||
|
||||
import "go_dreamfactory/pb"
|
||||
|
||||
type UserModel struct {
|
||||
pb.DB_UserData
|
||||
}
|
||||
|
1
modules/user/user_cache.go
Normal file
1
modules/user/user_cache.go
Normal file
@ -0,0 +1 @@
|
||||
package user
|
6
sys/cache/cache.go
vendored
6
sys/cache/cache.go
vendored
@ -17,12 +17,15 @@ type Cache struct {
|
||||
redis redis.ISys
|
||||
}
|
||||
|
||||
var rdsIns redis.ISys
|
||||
|
||||
//初始化 redis 对象
|
||||
func (this *Cache) init() (err error) {
|
||||
this.redis, err = redis.NewSys(
|
||||
redis.SetRedisType(redis.Redis_Cluster),
|
||||
redis.SetRedis_Cluster_Addr(this.options.Redis_Addr),
|
||||
redis.SetRedis_Cluster_Password(this.options.Redis_Password))
|
||||
rdsIns = this.redis
|
||||
return
|
||||
}
|
||||
|
||||
@ -30,3 +33,6 @@ func (this *Cache) init() (err error) {
|
||||
func (this *Cache) Redis() redis.ISys {
|
||||
return this.redis
|
||||
}
|
||||
func GetRds() redis.ISys {
|
||||
return rdsIns
|
||||
}
|
||||
|
1
sys/cache/core.go
vendored
1
sys/cache/core.go
vendored
@ -11,6 +11,7 @@ type (
|
||||
Redis() redis.ISys
|
||||
IUser //户模块的相关缓存接口
|
||||
IFriend //好友相关的缓存接口
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"go_dreamfactory/lego/core"
|
||||
"go_dreamfactory/lego/sys/mgo"
|
||||
)
|
||||
|
||||
@ -24,6 +25,12 @@ func (this *DB) init() (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *DB) Mgo() mgo.ISys {
|
||||
return this.mgo
|
||||
}
|
||||
|
||||
func (this *DB) Table(tableName string) *DB {
|
||||
this.mgo.Collection(core.SqlTable(tableName)).Database()
|
||||
return this
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ func TestCreate(t *testing.T) {
|
||||
Sid: 1,
|
||||
}
|
||||
|
||||
|
||||
|
||||
err := db.User_Create(user)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user