暂存pull dev

This commit is contained in:
zhaocy 2022-06-14 15:09:19 +08:00
parent b9d51378f3
commit 924da6fdd8
6 changed files with 65 additions and 29 deletions

View File

@ -2,33 +2,61 @@ package modules
import (
"fmt"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/sys/cache"
"google.golang.org/protobuf/proto"
)
type DBModel struct {
model proto.Message
*MComp_DBComp
pbData proto.Message
prefix string //rediskey前缀
tableName string
}
func (m *DBModel) Model(model proto.Message) *DBModel {
m.model = model
func NewDBModel(c *MComp_DBComp) *DBModel {
return &DBModel{MComp_DBComp: c}
}
func (m *DBModel) SetData(model proto.Message) *DBModel {
m.pbData = model
return m
}
func (m *DBModel) GetData() proto.Message {
return m.pbData
}
func (m *DBModel) SetPrefix(prefix string) {
m.prefix = prefix
}
func (m *DBModel) SetTableName(tableName string) {
m.tableName = tableName
}
func (m *DBModel) OnChange() {
}
func (m *DBModel) Set() {
func (m *DBModel) Set(key string, val interface{}) {
//调用写入mongo日志接口
}
//获取数据
func (m *DBModel) Get(key string) {
err := cache.GetRds().Get(key, m.model)
func (m *DBModel) Get(uid string) {
if m.prefix == "" {
log.Errorf("get data err,because prefix not setting")
return
}
err := cache.Redis().Get(fmt.Sprintf("%s:%s", m.prefix, uid), m.pbData)
if err != nil {
fmt.Printf("err:%v", err)
if err.Error() != redis.RedisNil.Error() {
log.Errorf("err:%v", err)
}
return
}
}

View File

@ -32,6 +32,7 @@ func (this *Model_Comp) Init(service core.IService, module core.IModule, comp co
this.ModuleCompBase.Init(service, module, comp, options)
this.Redis = cache.Redis()
this.DB = db.Mgo()
NewDBModel(this)
return
}
func (this *Model_Comp) Start() (err error) {

View File

@ -1,8 +1,7 @@
package modules
package user
import (
"fmt"
"go_dreamfactory/pb"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/db"
"os"
@ -23,10 +22,13 @@ func TestMain(m *testing.M) {
}
func TestInitData(t *testing.T) {
dbmodel := new(DBModel)
model := &pb.Cache_UserData{}
dbmodel.Model(model)
dbmodel.Get("user:62a2bd064098aa8ff982f2e2")
fmt.Printf("%v", model.SessionId)
func TestGet(t *testing.T) {
// userModel := NewUserModel()
// userModel.Set("1_62a729fc0e01ab2819553242",)
// fmt.Printf("%v", userModel.GetData().(*pb.Cache_UserData))
}
func TestSet(t *testing.T) {
// um := NewUserModel("1_62a729fc0e01ab2819553242")
}

View File

@ -1,8 +0,0 @@
package user
import "go_dreamfactory/pb"
type UserModel struct {
pb.DB_UserData
}

View File

@ -0,0 +1,19 @@
package user
import (
"go_dreamfactory/modules"
"go_dreamfactory/pb"
)
type UserModel struct {
modules.DBModel
}
func NewUserModel() *UserModel {
model := new(UserModel)
model.SetPrefix("user") //每个Model有自己的唯一key
model.SetTableName("user")
model.SetData(&pb.Cache_UserData{}) //设置具体数据类型
// model.Get(uid) //获取model数据
return model
}

6
sys/cache/cache.go vendored
View File

@ -17,15 +17,12 @@ 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
}
@ -33,6 +30,3 @@ func (this *Cache) init() (err error) {
func (this *Cache) Redis() redis.ISys {
return this.redis
}
func GetRds() redis.ISys {
return rdsIns
}