test-errcode
Gordon 2 years ago
parent 640960a87b
commit 0d3984233b

@ -5,7 +5,7 @@ import (
"Open_IM/pkg/common/db/mongoDB"
"Open_IM/pkg/common/db/mysql"
"context"
"github.com/dtm-labs/rockscache"
_ "github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
@ -19,8 +19,8 @@ type GroupInterface interface {
Take(ctx context.Context, groupID string) (group *mysql.Group, err error)
}
type GroupModel struct {
db mysql.GroupModelInterface
type GroupController struct {
db DataBase
cache *cache.GroupCache
mongo *mongoDB.Client
}
@ -31,29 +31,40 @@ type DataBase interface {
Take(ctx context.Context, groupID string) (group *mysql.Group, err error)
DeleteTx(ctx context.Context, groupIDs []string) error
}
type Database struct {
type MySqlDatabase struct {
mysql.GroupModelInterface
}
func NewGroupModel(db mysql.GroupModelInterface, rdb redis.UniversalClient, mdb *mongo.Client) *GroupModel {
var groupModel GroupModel
groupModel.db = db
groupModel.cache = cache.NewGroupCache(rdb, db, rockscache.Options{
DisableCacheRead: false,
StrongConsistency: true,
})
groupModel.mongo = mongoDB.NewMongoClient(mdb)
return &groupModel
func (m *MySqlDatabase) Delete(ctx context.Context, groupIDs []string) error {
panic("implement me")
}
func NewMySqlDatabase(db mysql.GroupModelInterface) DataBase {
return &MySqlDatabase{db}
}
func (m *MySqlDatabase) DeleteTx(ctx context.Context, groupIDs []string) error {
return nil
}
func NewGroupController(groupModel mysql.GroupModelInterface, rdb redis.UniversalClient, mdb *mongo.Client) *GroupController {
return &GroupController{db: NewMySqlDatabase(groupModel)}
//groupModel.cache = cache.NewGroupCache(rdb, db, rockscache.Options{
// DisableCacheRead: false,
// StrongConsistency: true,
//})
//groupModel.mongo = mongoDB.NewMongoClient(mdb)
//return &groupModel
}
func (g *GroupModel) Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) {
func (g *GroupController) Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) {
return g.cache.GetGroupsInfoFromCache(ctx, groupIDs)
}
func (g *GroupModel) Create(ctx context.Context, groups []*mysql.Group) error {
func (g *GroupController) Create(ctx context.Context, groups []*mysql.Group) error {
return g.db.Create(ctx, groups)
}
func (g *GroupModel) Delete(ctx context.Context, groupIDs []string) error {
func (g *GroupController) Delete(ctx context.Context, groupIDs []string) error {
err := g.db.DB.Transaction(func(tx *gorm.DB) error {
if err := g.db.Delete(ctx, groupIDs, tx); err != nil {
return err
@ -66,11 +77,11 @@ func (g *GroupModel) Delete(ctx context.Context, groupIDs []string) error {
return err
}
func (g *GroupModel) Take(ctx context.Context, groupID string) (group *mysql.Group, err error) {
func (g *GroupController) Take(ctx context.Context, groupID string) (group *mysql.Group, err error) {
return g.cache.GetGroupInfoFromCache(ctx, groupID)
}
func (g *GroupModel) Update(ctx context.Context, groups []*mysql.Group) error {
func (g *GroupController) Update(ctx context.Context, groups []*mysql.Group) error {
err := g.db.DB.Transaction(func(tx *gorm.DB) error {
if err := g.db.Update(ctx, groups, tx); err != nil {
return err

Loading…
Cancel
Save