mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-01 07:35:58 +08:00
proto modify
This commit is contained in:
Vendored
+1
-33
@@ -25,33 +25,6 @@ const (
|
||||
)
|
||||
|
||||
type GroupCache interface {
|
||||
GetGroupsInfo(ctx context.Context, groupIDs []string, fn func(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)) (groups []*relationTb.GroupModel, err error)
|
||||
DelGroupsInfo(ctx context.Context, groupID string) (err error)
|
||||
GetGroupInfo(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)) (group *relationTb.GroupModel, err error)
|
||||
DelGroupInfo(ctx context.Context, groupID string) (err error)
|
||||
|
||||
BatchDelJoinedSuperGroupIDs(ctx context.Context, userIDs []string, fn func(ctx context.Context, userIDs []string) error) (err error)
|
||||
|
||||
GetJoinedSuperGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error)) (joinedSuperGroupIDs []string, err error)
|
||||
DelJoinedSuperGroupIDs(ctx context.Context, userID string) (err error)
|
||||
|
||||
GetGroupMembersHash(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (hashCodeUint64 uint64, err error)) (hashCodeUint64 uint64, err error)
|
||||
DelGroupMembersHash(ctx context.Context, groupID string) (err error)
|
||||
|
||||
GetGroupMemberIDs(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (groupMemberIDs []string, err error)) (groupMemberIDs []string, err error)
|
||||
DelGroupMemberIDs(ctx context.Context, groupID string) error
|
||||
|
||||
GetJoinedGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedGroupIDs []string, err error)) (joinedGroupIDs []string, err error)
|
||||
DelJoinedGroupIDs(ctx context.Context, userID string) (err error)
|
||||
|
||||
GetGroupMemberInfo(ctx context.Context, groupID, userID string, fn func(ctx context.Context, groupID, userID string) (groupMember *relationTb.GroupMemberModel, err error)) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
DelGroupMemberInfo(ctx context.Context, groupID, userID string) (err error)
|
||||
|
||||
GetGroupMemberNum(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (num int, err error)) (num int, err error)
|
||||
DelGroupMemberNum(ctx context.Context, groupID string) (err error)
|
||||
}
|
||||
|
||||
type GroupCacheRedisInterface interface {
|
||||
GetGroupsInfo(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
GetGroupInfo(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
BatchDelJoinedSuperGroupIDs(ctx context.Context, userIDs []string) (err error)
|
||||
@@ -76,21 +49,16 @@ type GroupCacheRedis struct {
|
||||
groupRequest relationTb.GroupRequestModelInterface
|
||||
mongoDB unrelation2.SuperGroupModelInterface
|
||||
expireTime time.Duration
|
||||
redisClient *RedisClient
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewGroupCacheRedis(rdb redis.UniversalClient, groupDB relationTb.GroupModelInterface, groupMemberDB relationTb.GroupMemberModelInterface, groupRequestDB relationTb.GroupRequestModelInterface, mongoClient unrelation2.SuperGroupModelInterface, opts rockscache.Options) GroupCacheRedisInterface {
|
||||
return &GroupCacheRedis{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
|
||||
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
|
||||
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB,
|
||||
mongoDB: mongoClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) getRedisClient() *RedisClient {
|
||||
return g.redisClient
|
||||
}
|
||||
|
||||
func (g *GroupCacheRedis) getGroupInfoKey(groupID string) string {
|
||||
return groupInfoKey + groupID
|
||||
}
|
||||
|
||||
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/config"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewRedis() (redis.UniversalClient, error) {
|
||||
var rdb redis.UniversalClient
|
||||
if config.Config.Redis.EnableCluster {
|
||||
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: config.Config.Redis.DBAddress,
|
||||
Username: config.Config.Redis.DBUserName,
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
PoolSize: 50,
|
||||
})
|
||||
} else {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: config.Config.Redis.DBAddress[0],
|
||||
Username: config.Config.Redis.DBUserName,
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
DB: 0, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
err := rdb.Ping(ctx).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("redis ping %w", err)
|
||||
}
|
||||
return rdb, nil
|
||||
}
|
||||
Vendored
+109
-114
@@ -50,7 +50,6 @@ type Cache interface {
|
||||
SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error
|
||||
SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error)
|
||||
GetUserMinSeq(ctx context.Context, userID string) (int64, error)
|
||||
|
||||
SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error)
|
||||
GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error)
|
||||
GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
@@ -58,28 +57,23 @@ type Cache interface {
|
||||
IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error
|
||||
SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error
|
||||
|
||||
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
|
||||
|
||||
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
|
||||
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error
|
||||
GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||
SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error)
|
||||
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error
|
||||
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
|
||||
HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error)
|
||||
GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error)
|
||||
GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error)
|
||||
GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *sdkws.SignalInviteReq, err error)
|
||||
GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error)
|
||||
DelUserSignalList(ctx context.Context, userID string) error
|
||||
DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error
|
||||
|
||||
SetGetuiToken(ctx context.Context, token string, expireTime int64) error
|
||||
GetGetuiToken(ctx context.Context) (string, error)
|
||||
SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error
|
||||
GetGetuiTaskID(ctx context.Context) (string, error)
|
||||
|
||||
SetSendMsgStatus(ctx context.Context, id string, status int32) error
|
||||
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
|
||||
SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error)
|
||||
@@ -98,75 +92,76 @@ type Cache interface {
|
||||
UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||||
}
|
||||
|
||||
func NewMsgCache(client redis.UniversalClient) Cache {
|
||||
return &msgCache{rdb: client}
|
||||
func NewCache(client redis.UniversalClient) Cache {
|
||||
return &cache{rdb: client}
|
||||
}
|
||||
|
||||
type msgCache struct {
|
||||
type cache struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
func (c *cache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
func (c *cache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userIncrSeq+userID, maxSeq, 0).Err())
|
||||
func (c *cache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, userIncrSeq+userID, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userMinSeq+userID, minSeq, 0).Err())
|
||||
func (c *cache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, userMinSeq+userID, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userMinSeq+userID).Int64())
|
||||
func (c *cache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, userMinSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||||
func (c *cache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||||
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
return utils.Wrap1(c.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
func (c *cache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||||
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
||||
return utils.Wrap2(c.rdb.Get(ctx, key).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMaxSeq+groupID).Int64())
|
||||
func (c *cache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, groupMaxSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
func (c *cache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
func (c *cache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
key := groupMaxSeq + groupID
|
||||
seq, err := m.rdb.Incr(ctx, key).Uint64()
|
||||
seq, err := c.rdb.Incr(ctx, key).Uint64()
|
||||
return int64(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||||
func (c *cache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||||
key := groupMaxSeq + groupID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, maxSeq, 0).Err())
|
||||
return utils.Wrap1(c.rdb.Set(ctx, key, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||||
func (c *cache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||||
key := groupMinSeq + groupID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
return utils.Wrap1(c.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||
func (c *cache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, token, flag).Err())
|
||||
return utils.Wrap1(c.rdb.HSet(ctx, key, token, flag).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||||
func (c *cache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platformID
|
||||
m, err := m.rdb.HGetAll(ctx, key).Result()
|
||||
m, err := c.rdb.HGetAll(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
@@ -177,26 +172,26 @@ func (m *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID
|
||||
return mm, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
func (c *cache) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
mm := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
mm[k] = v
|
||||
}
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, mm).Err())
|
||||
return utils.Wrap1(c.rdb.HSet(ctx, key, mm).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, key, fields...).Err())
|
||||
func (c *cache) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
return utils.Wrap1(c.rdb.HDel(ctx, key, fields...).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) {
|
||||
func (c *cache) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) {
|
||||
var errResult error
|
||||
for _, v := range seqList {
|
||||
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(v))
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
result, err := c.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
errResult = err
|
||||
failedSeqList = append(failedSeqList, v)
|
||||
@@ -215,8 +210,8 @@ func (m *msgCache) GetMessagesBySeq(ctx context.Context, userID string, seqList
|
||||
return seqMsg, failedSeqList, errResult
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error) {
|
||||
pipe := m.rdb.Pipeline()
|
||||
func (c *cache) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error) {
|
||||
pipe := c.rdb.Pipeline()
|
||||
var failedList []pbChat.MsgDataToMQ
|
||||
for _, msg := range msgList {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
||||
@@ -236,18 +231,18 @@ func (m *msgCache) SetMessageToCache(ctx context.Context, userID string, msgList
|
||||
return 0, err
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
|
||||
func (c *cache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
|
||||
for _, msg := range msgList {
|
||||
if err := m.rdb.Del(ctx, messageCache+userID+"_"+strconv.Itoa(int(msg.MsgData.Seq))).Err(); err != nil {
|
||||
if err := c.rdb.Del(ctx, messageCache+userID+"_"+strconv.Itoa(int(msg.MsgData.Seq))).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||||
func (c *cache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||||
key := messageCache + userID + "_" + "*"
|
||||
vals, err := m.rdb.Keys(ctx, key).Result()
|
||||
vals, err := c.rdb.Keys(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
}
|
||||
@@ -255,14 +250,14 @@ func (m *msgCache) CleanUpOneUserAllMsg(ctx context.Context, userID string) erro
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
for _, v := range vals {
|
||||
if err := m.rdb.Del(ctx, v).Err(); err != nil {
|
||||
if err := c.rdb.Del(ctx, v).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||||
func (c *cache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||||
req := &pbRtc.SignalReq{}
|
||||
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
@@ -291,16 +286,16 @@ func (m *msgCache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pus
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
keyList := SignalListCache + userID
|
||||
err = m.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
|
||||
err = c.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
err = m.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
|
||||
err = c.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
key := SignalCache + msg.ClientMsgID
|
||||
err = m.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||||
err = c.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
@@ -309,16 +304,16 @@ func (m *msgCache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pus
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
bytes, err := m.rdb.Get(ctx, SignalCache+clientMsgID).Bytes()
|
||||
func (c *cache) GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *sdkws.SignalInviteReq, err error) {
|
||||
bytes, err := c.rdb.Get(ctx, SignalCache+clientMsgID).Bytes()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
req := &pbRtc.SignalReq{}
|
||||
req := &sdkws.SignalReq{}
|
||||
if err = proto.Unmarshal(bytes, req); err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo = &pbRtc.SignalInviteReq{}
|
||||
invitationInfo = &sdkws.SignalInviteReq{}
|
||||
switch req2 := req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite:
|
||||
invitationInfo.Invitation = req2.Invite.Invitation
|
||||
@@ -330,26 +325,26 @@ func (m *msgCache) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clie
|
||||
return invitationInfo, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
key, err := m.rdb.LPop(ctx, SignalListCache+userID).Result()
|
||||
func (c *cache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error) {
|
||||
key, err := c.rdb.LPop(ctx, SignalListCache+userID).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo, err = m.GetSignalInfoFromCacheByClientMsgID(ctx, key)
|
||||
invitationInfo, err = c.GetSignalInvitationInfoByClientMsgID(ctx, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return invitationInfo, m.DelUserSignalList(ctx, userID)
|
||||
return invitationInfo, utils.Wrap1(c.DelUserSignalList(ctx, userID))
|
||||
}
|
||||
|
||||
func (m *msgCache) DelUserSignalList(ctx context.Context, userID string) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, SignalListCache+userID).Err())
|
||||
func (c *cache) DelUserSignalList(ctx context.Context, userID string) error {
|
||||
return utils.Wrap1(c.rdb.Del(ctx, SignalListCache+userID).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error {
|
||||
func (c *cache) DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error {
|
||||
for _, seq := range seqList {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(seq))
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
result, err := c.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
continue
|
||||
@@ -365,74 +360,74 @@ func (m *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqList [
|
||||
if err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
if err := m.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||
if err := c.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err())
|
||||
func (c *cache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGetuiToken(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiToken).Result())
|
||||
func (c *cache) GetGetuiToken(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, getuiToken).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
|
||||
func (c *cache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiTaskID).Result())
|
||||
func (c *cache) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, getuiTaskID).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, sendMsgFailedFlag+id, status, time.Hour*24).Err())
|
||||
func (c *cache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, sendMsgFailedFlag+id, status, time.Hour*24).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||||
result, err := m.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
||||
func (c *cache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||||
result, err := c.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
||||
return int32(result), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, FcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
||||
func (c *cache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, FcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Result())
|
||||
func (c *cache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Err())
|
||||
func (c *cache) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||||
return utils.Wrap1(c.rdb.Del(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
seq, err := m.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
||||
func (c *cache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
seq, err := c.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
||||
return int(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userBadgeUnreadCountSum+userID, value, 0).Err())
|
||||
func (c *cache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
|
||||
return utils.Wrap1(c.rdb.Set(ctx, userBadgeUnreadCountSum+userID, value, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userBadgeUnreadCountSum+userID).Int())
|
||||
func (c *cache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
return utils.Wrap2(c.rdb.Get(ctx, userBadgeUnreadCountSum+userID).Int())
|
||||
}
|
||||
|
||||
func (m *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
func (c *cache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return utils.Wrap1(m.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||||
return utils.Wrap1(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
func (c *cache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return utils.Wrap1(m.rdb.Del(ctx, key).Err())
|
||||
return utils.Wrap1(c.rdb.Del(ctx, key).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||||
func (c *cache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||||
switch sessionType {
|
||||
case constant.SingleChatType:
|
||||
return "EX_SINGLE_" + clientMsgID
|
||||
@@ -446,30 +441,30 @@ func (m *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType in
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *msgCache) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||||
n, err := m.rdb.Exists(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||||
func (c *cache) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||||
n, err := c.rdb.Exists(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||||
if err != nil {
|
||||
return false, utils.Wrap(err, "")
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||||
func (c *cache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||||
return utils.Wrap1(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||||
return utils.Wrap2(m.rdb.Expire(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
|
||||
func (c *cache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||||
return utils.Wrap2(c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||||
return utils.Wrap2(m.rdb.HGet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
|
||||
func (c *cache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||||
return utils.Wrap2(c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||||
return utils.Wrap2(m.rdb.HGetAll(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
|
||||
func (c *cache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||||
return utils.Wrap2(c.rdb.HGetAll(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||||
func (c *cache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||||
return utils.Wrap1(c.rdb.HDel(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||||
}
|
||||
|
||||
Vendored
-59
@@ -1,59 +0,0 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/tokenverify"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
type Token interface {
|
||||
//结果为空 不返回错误
|
||||
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||
//创建token
|
||||
CreateToken(ctx context.Context, userID string, platformID int) (string, error)
|
||||
}
|
||||
|
||||
type TokenRedis struct {
|
||||
redisClient *RedisClient
|
||||
accessSecret string
|
||||
accessExpire int64
|
||||
}
|
||||
|
||||
func NewTokenRedis(redisClient *RedisClient, accessSecret string, accessExpire int64) *TokenRedis {
|
||||
return &TokenRedis{redisClient, accessSecret, accessExpire}
|
||||
}
|
||||
|
||||
// 结果为空 不返回错误
|
||||
func (t *TokenRedis) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||
return t.redisClient.GetTokensWithoutError(ctx, userID, platform)
|
||||
}
|
||||
|
||||
// 创建token
|
||||
func (t *TokenRedis) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||
tokens, err := t.redisClient.GetTokensWithoutError(ctx, userID, platform)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var deleteTokenKey []string
|
||||
for k, v := range tokens {
|
||||
_, err = tokenverify.GetClaimFromToken(k)
|
||||
if err != nil || v != constant.NormalToken {
|
||||
deleteTokenKey = append(deleteTokenKey, k)
|
||||
}
|
||||
}
|
||||
if len(deleteTokenKey) != 0 {
|
||||
err := t.redisClient.DeleteTokenByUidPid(ctx, userID, platform, deleteTokenKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
claims := tokenverify.BuildClaims(userID, platform, t.accessExpire)
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString([]byte(t.accessSecret))
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return tokenString, t.redisClient.AddTokenFlag(ctx, userID, constant.PlatformNameToID(platform), tokenString, constant.NormalToken)
|
||||
}
|
||||
Vendored
+6
-7
@@ -25,17 +25,16 @@ type UserCache interface {
|
||||
type UserCacheRedis struct {
|
||||
userDB *relation.UserGorm
|
||||
|
||||
expireTime time.Duration
|
||||
redisClient *RedisClient
|
||||
rcClient *rockscache.Client
|
||||
expireTime time.Duration
|
||||
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewUserCacheRedis(rdb redis.UniversalClient, userDB *relation.UserGorm, options rockscache.Options) *UserCacheRedis {
|
||||
return &UserCacheRedis{
|
||||
userDB: userDB,
|
||||
expireTime: userExpireTime,
|
||||
redisClient: NewRedisClient(rdb),
|
||||
rcClient: rockscache.NewClient(rdb, options),
|
||||
userDB: userDB,
|
||||
expireTime: userExpireTime,
|
||||
rcClient: rockscache.NewClient(rdb, options),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user