mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-08-19 11:22:07 +08:00
groupdb
This commit is contained in:
parent
86157b4f7a
commit
d21b1e6e78
@ -2,50 +2,34 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"Open_IM/pkg/common/db/cache"
|
"Open_IM/pkg/common/db/cache"
|
||||||
|
"Open_IM/pkg/common/db/mongo"
|
||||||
"Open_IM/pkg/common/db/mysql"
|
"Open_IM/pkg/common/db/mysql"
|
||||||
"Open_IM/pkg/common/trace_log"
|
"Open_IM/pkg/common/trace_log"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/dtm-labs/rockscache"
|
//"github.com/dtm-labs/rockscache"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"time"
|
//"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GroupModel struct {
|
type GroupModel struct {
|
||||||
strongRc *cache.RcClient
|
|
||||||
weakRc *cache.RcClient
|
|
||||||
db *mysql.Group
|
db *mysql.Group
|
||||||
rdb *cache.RedisClient
|
cache *cache.GroupCache
|
||||||
|
mongo *mongo.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
const GroupExpireTime = time.Second * 300 * 60
|
|
||||||
const RandomExpireAdjustment = 0.2
|
|
||||||
|
|
||||||
//cache key
|
func NewGroupModel() {
|
||||||
const groupInfoCache = "GROUP_INFO_CACHE:"
|
|
||||||
|
|
||||||
func NewGroupModel(ctx context.Context) {
|
|
||||||
var groupModel GroupModel
|
var groupModel GroupModel
|
||||||
redisClient := cache.InitRedis(ctx)
|
redisClient := cache.InitRedis()
|
||||||
rdb := cache.NewRedisClient(redisClient)
|
rdb := cache.NewRedisClient(redisClient)
|
||||||
groupModel.rdb = rdb
|
|
||||||
groupModel.db = mysql.NewGroupDB()
|
groupModel.db = mysql.NewGroupDB()
|
||||||
groupModel.strongRc = cache.NewRcClient(redisClient, GroupExpireTime, rockscache.Options{
|
//mgo := mongo.In()
|
||||||
RandomExpireAdjustment: RandomExpireAdjustment,
|
|
||||||
DisableCacheRead: false,
|
|
||||||
DisableCacheDelete: false,
|
|
||||||
StrongConsistency: true,
|
|
||||||
})
|
|
||||||
groupModel.weakRc = cache.NewRcClient(redisClient, GroupExpireTime, rockscache.Options{
|
|
||||||
RandomExpireAdjustment: RandomExpireAdjustment,
|
|
||||||
DisableCacheRead: false,
|
|
||||||
DisableCacheDelete: false,
|
|
||||||
StrongConsistency: false,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupModel) Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) {
|
func (g *GroupModel) Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) {
|
||||||
|
g.cache.Client.
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
group, err := g.getGroupInfoFromCache(ctx, groupID)
|
group, err := g.getGroupInfoFromCache(ctx, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -61,21 +45,16 @@ func (g *GroupModel) Create(ctx context.Context, groups []*mysql.Group) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupModel) Delete(ctx context.Context, groupIDs []string) error {
|
func (g *GroupModel) Delete(ctx context.Context, groupIDs []string) error {
|
||||||
tx := g.db.DB.Begin()
|
err := g.db.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := g.db.Delete(ctx, groupIDs); err != nil {
|
if err := g.db.Delete(ctx, groupIDs, tx); err != nil {
|
||||||
tx.Commit()
|
return err
|
||||||
return err
|
}
|
||||||
}
|
if err := g.deleteGroupsInCache(ctx, groupIDs); err != nil {
|
||||||
if err := g.deleteGroupsInCache(ctx, groupIDs); err != nil {
|
return err
|
||||||
tx.Rollback()
|
}
|
||||||
return err
|
return nil
|
||||||
}
|
})
|
||||||
tx.Commit()
|
return err
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupModel) getGroupCacheKey(groupID string) string {
|
|
||||||
return groupInfoCache + groupID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupModel) deleteGroupsInCache(ctx context.Context, groupIDs []string) error {
|
func (g *GroupModel) deleteGroupsInCache(ctx context.Context, groupIDs []string) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user