From a55e27c581af914d3891fc7a22bd8cd0d79ffe0a Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Tue, 17 Jan 2023 14:40:20 +0800 Subject: [PATCH] groupdb --- pkg/common/db/cache/group.go | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pkg/common/db/cache/group.go diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go new file mode 100644 index 000000000..ec439ebcd --- /dev/null +++ b/pkg/common/db/cache/group.go @@ -0,0 +1,72 @@ +package cache + +import ( + "Open_IM/pkg/common/db/mysql" + "Open_IM/pkg/common/trace_log" + "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 { + Client *Client + db *mysql.Group + expireTime time.Duration +} + +func NewGroupRc(rdb redis.UniversalClient, db *mysql.Group, opts rockscache.Options) GroupCache { + rcClient := newClient(rdb, opts) + return GroupCache{Client: rcClient, expireTime: GroupExpireTime} +} + +func (g *GroupCache) GetGroupsInfoFromCache(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) { + for _, groupID := range groupIDs { + group, err := g.GetGroupInfoFromCache(ctx, groupID) + if err != nil { + return nil, err + } + groups = append(groups, group) + } + return groups, nil +} + +func (g *GroupCache) GetGroupInfoFromCache(ctx context.Context, groupID string) (group *mysql.Group, err error) { + getGroup := func() (string, error) { + groupInfo, err := g.db.Take(ctx, groupID) + if err != nil { + return "", utils.Wrap(err, "") + } + bytes, err := json.Marshal(groupInfo) + if err != nil { + return "", utils.Wrap(err, "") + } + return string(bytes), nil + } + group = &mysql.Group{} + defer func() { + trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group) + }() + groupStr, err := g.Client.rcClient.Fetch(g.getGroupInfoCacheKey(groupID), g.expireTime, getGroup) + if err != nil { + return nil, utils.Wrap(err, "") + } + err = json.Unmarshal([]byte(groupStr), group) + return group, utils.Wrap(err, "") +} + +func (g *GroupCache) DelGroupInfoFromCache(ctx context.Context, groupID string) (err error) { + defer func() { + trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID) + }() + return g.Client.rcClient.TagAsDeleted(g.getGroupInfoCacheKey(groupID)) +} + +func (g *GroupCache) getGroupInfoCacheKey(groupID string) string { + return groupInfoCacheKey + groupID +}