Files
open-im-server/pkg/common/db/cache/group.go
T

103 lines
3.0 KiB
Go
Raw Normal View History

2023-01-17 14:40:20 +08:00
package cache
import (
2023-01-28 13:19:36 +08:00
"Open_IM/pkg/common/db/relation"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2023-01-17 14:40:20 +08:00
"Open_IM/pkg/utils"
"context"
"encoding/json"
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"time"
)
const GroupExpireTime = time.Second * 60 * 60 * 12
const groupInfoCacheKey = "GROUP_INFO_CACHE:"
type GroupCache struct {
2023-01-29 10:31:01 +08:00
group *relation.Group
groupMember *relation.GroupMember
groupRequest *relation.GroupRequest
expireTime time.Duration
redisClient *RedisClient
rcClient *rockscache.Client
2023-01-17 14:40:20 +08:00
}
2023-01-29 10:31:01 +08:00
func NewGroupCache(rdb redis.UniversalClient, groupDB *relation.Group, groupMemberDB *relation.GroupMember, groupRequestDB *relation.GroupRequest, opts rockscache.Options) *GroupCache {
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: GroupExpireTime, group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb)}
2023-01-17 16:36:34 +08:00
}
func (g *GroupCache) getRedisClient() *RedisClient {
return g.redisClient
2023-01-17 14:40:20 +08:00
}
2023-01-29 10:31:01 +08:00
func (g *GroupCache) GetGroupsInfo(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) {
2023-01-17 14:40:20 +08:00
for _, groupID := range groupIDs {
2023-01-29 10:31:01 +08:00
group, err := g.GetGroupInfo(ctx, groupID)
2023-01-17 14:40:20 +08:00
if err != nil {
return nil, err
}
groups = append(groups, group)
}
return groups, nil
}
2023-01-29 10:31:01 +08:00
func (g *GroupCache) GetGroupInfo(ctx context.Context, groupID string) (group *relation.Group, err error) {
2023-01-17 14:40:20 +08:00
getGroup := func() (string, error) {
2023-01-29 10:31:01 +08:00
groupInfo, err := g.group.Take(ctx, groupID)
2023-01-17 14:40:20 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(groupInfo)
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
}
2023-01-28 13:19:36 +08:00
group = &relation.Group{}
2023-01-17 14:40:20 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
2023-01-17 14:40:20 +08:00
}()
2023-01-17 16:36:34 +08:00
groupStr, err := g.rcClient.Fetch(g.getGroupInfoCacheKey(groupID), g.expireTime, getGroup)
2023-01-17 14:40:20 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
err = json.Unmarshal([]byte(groupStr), group)
return group, utils.Wrap(err, "")
}
2023-01-29 10:31:01 +08:00
func (g *GroupCache) DelGroupInfo(ctx context.Context, groupID string) (err error) {
2023-01-17 14:40:20 +08:00
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID)
2023-01-17 14:40:20 +08:00
}()
2023-01-17 16:36:34 +08:00
return g.rcClient.TagAsDeleted(g.getGroupInfoCacheKey(groupID))
}
2023-01-29 10:31:01 +08:00
func (g *GroupCache) DelGroupsInfo(ctx context.Context, groupIDs []string) error {
2023-01-17 16:36:34 +08:00
for _, groupID := range groupIDs {
2023-01-29 10:31:01 +08:00
if err := g.DelGroupInfo(ctx, groupID); err != nil {
2023-01-17 16:36:34 +08:00
return err
}
}
return nil
2023-01-17 14:40:20 +08:00
}
func (g *GroupCache) getGroupInfoCacheKey(groupID string) string {
return groupInfoCacheKey + groupID
}
2023-01-29 10:31:01 +08:00
func (g *GroupCache) DelJoinedSuperGroupIDs(ctx context.Context, userIDs []string) (err error) {
for _, userID := range userIDs {
if err := g.rcClient.TagAsDeleted(joinedSuperGroupListCache + userID); err != nil {
return err
}
}
}
func (g *GroupCache) DelJoinedSuperGroupID(ctx context.Context, userID string) (err error) {
defer func() {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID)
2023-01-29 10:31:01 +08:00
}()
return g.rcClient.TagAsDeleted(joinedSuperGroupListCache + userID)
}