From 69cfaf0737496a4c60cb59e591f29a8b9588bd7f Mon Sep 17 00:00:00 2001 From: zhaocy Date: Mon, 13 Jun 2022 16:51:03 +0800 Subject: [PATCH] =?UTF-8?q?model=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/dbmodel.go | 34 ++++++++++++++++++++++++++++++++++ modules/dbmodel_test.go | 32 ++++++++++++++++++++++++++++++++ modules/user/model_user.go | 8 ++++++++ modules/user/user_cache.go | 1 + sys/cache/cache.go | 6 ++++++ sys/cache/core.go | 1 + sys/db/db.go | 7 +++++++ sys/db/user_test.go | 2 ++ 8 files changed, 91 insertions(+) create mode 100644 modules/dbmodel.go create mode 100644 modules/dbmodel_test.go create mode 100644 modules/user/model_user.go create mode 100644 modules/user/user_cache.go diff --git a/modules/dbmodel.go b/modules/dbmodel.go new file mode 100644 index 000000000..30e6c0b9a --- /dev/null +++ b/modules/dbmodel.go @@ -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 + } +} diff --git a/modules/dbmodel_test.go b/modules/dbmodel_test.go new file mode 100644 index 000000000..c9c6591c6 --- /dev/null +++ b/modules/dbmodel_test.go @@ -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) +} diff --git a/modules/user/model_user.go b/modules/user/model_user.go new file mode 100644 index 000000000..58c565e64 --- /dev/null +++ b/modules/user/model_user.go @@ -0,0 +1,8 @@ +package user + +import "go_dreamfactory/pb" + +type UserModel struct { + pb.DB_UserData +} + diff --git a/modules/user/user_cache.go b/modules/user/user_cache.go new file mode 100644 index 000000000..47068ebab --- /dev/null +++ b/modules/user/user_cache.go @@ -0,0 +1 @@ +package user \ No newline at end of file diff --git a/sys/cache/cache.go b/sys/cache/cache.go index 924bbaa54..4d14fe40b 100644 --- a/sys/cache/cache.go +++ b/sys/cache/cache.go @@ -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 +} diff --git a/sys/cache/core.go b/sys/cache/core.go index d372608c3..2d405ab00 100644 --- a/sys/cache/core.go +++ b/sys/cache/core.go @@ -11,6 +11,7 @@ type ( Redis() redis.ISys IUser //户模块的相关缓存接口 IFriend //好友相关的缓存接口 + } ) diff --git a/sys/db/db.go b/sys/db/db.go index e6e6086cd..0736b4457 100644 --- a/sys/db/db.go +++ b/sys/db/db.go @@ -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 +} diff --git a/sys/db/user_test.go b/sys/db/user_test.go index 2b07fea2e..9a1acef2e 100644 --- a/sys/db/user_test.go +++ b/sys/db/user_test.go @@ -16,6 +16,8 @@ func TestCreate(t *testing.T) { Sid: 1, } + + err := db.User_Create(user) require.Nil(t, err) }