parent
e0d05e1fa8
commit
40470bc320
@ -0,0 +1,64 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
blackIDsKey = "BLACK_IDS:"
|
||||
blackExpireTime = time.Second * 60 * 60 * 12
|
||||
)
|
||||
|
||||
type BlackCache struct {
|
||||
blackDB *relation.Black
|
||||
expireTime time.Duration
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewBlackCache(blackDB *relation.Black) *BlackCache {
|
||||
return &BlackCache{
|
||||
blackDB: nil,
|
||||
expireTime: 0,
|
||||
rcClient: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BlackCache) getBlackIDsKey(ownerUserID string) string {
|
||||
return blackIDsKey + ownerUserID
|
||||
}
|
||||
|
||||
func (b *BlackCache) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
|
||||
getBlackIDList := func() (string, error) {
|
||||
blackIDs, err := b.blackDB.GetBlackIDs(ctx, userID)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
bytes, err := json.Marshal(blackIDs)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "userID", userID, "blackIDList", blackIDs)
|
||||
}()
|
||||
blackIDListStr, err := b.rcClient.Fetch(blackListCache+userID, time.Second*30*60, getBlackIDList)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
err = json.Unmarshal([]byte(blackIDListStr), &blackIDs)
|
||||
return blackIDs, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func (b *BlackCache) DelBlackIDListFromCache(ctx context.Context, userID string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ctx", ctx)
|
||||
}()
|
||||
return b.rcClient.TagAsDeleted(blackListCache + userID)
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
friendExpireTime = time.Second * 60 * 60 * 12
|
||||
friendIDsKey = "FRIEND_IDS:"
|
||||
)
|
||||
|
||||
type FriendCache struct {
|
||||
friendDB *relation.Friend
|
||||
expireTime time.Duration
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewFriendCache(rdb redis.UniversalClient, friendDB *relation.Friend, options rockscache.Options) *FriendCache {
|
||||
return &FriendCache{
|
||||
friendDB: friendDB,
|
||||
expireTime: friendExpireTime,
|
||||
rcClient: rockscache.NewClient(rdb, options),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FriendCache) getFriendRelationKey(ownerUserID string) string {
|
||||
return friendIDsKey + ownerUserID
|
||||
}
|
||||
|
||||
func (f *FriendCache) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
|
||||
getFriendIDList := func() (string, error) {
|
||||
friendIDList, err := f.friendDB.GetFriendIDs(ctx, ownerUserID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
bytes, err := json.Marshal(friendIDList)
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendIDs", friendIDs)
|
||||
}()
|
||||
friendIDListStr, err := f.rcClient.Fetch(f.getFriendRelationKey(ownerUserID), f.expireTime, getFriendIDList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(friendIDListStr), &friendIDs)
|
||||
return friendIDs, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func (f *FriendCache) DelFriendIDs(ctx context.Context, ownerUserID string) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
|
||||
}()
|
||||
return f.rcClient.TagAsDeleted(f.getFriendRelationKey(ownerUserID))
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package localcache
|
||||
|
||||
import "google.golang.org/grpc"
|
||||
|
||||
type GroupLocalCache struct {
|
||||
cache map[string]GroupMemberIDsHash
|
||||
rpc *grpc.ClientConn
|
||||
}
|
||||
|
||||
type GroupMemberIDsHash struct {
|
||||
MemberListHash uint64
|
||||
UserIDs []string
|
||||
}
|
||||
|
||||
func NewGroupMemberIDsLocalCache(rpc *grpc.ClientConn) GroupLocalCache {
|
||||
return GroupLocalCache{
|
||||
cache: make(map[string]GroupMemberIDsHash, 0),
|
||||
rpc: rpc,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupMemberIDsHash) GetGroupMemberIDs(groupID string) []string {
|
||||
return []string{}
|
||||
}
|
Loading…
Reference in new issue