You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
440 lines
13 KiB
440 lines
13 KiB
1 year ago
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
6 months ago
|
package redis
|
||
1 year ago
|
|
||
|
import (
|
||
|
"context"
|
||
1 year ago
|
"errors"
|
||
9 months ago
|
"github.com/gogo/protobuf/jsonpb"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
|
||
9 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||
7 months ago
|
"github.com/openimsdk/protocol/constant"
|
||
|
"github.com/openimsdk/protocol/sdkws"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
|
"github.com/openimsdk/tools/log"
|
||
|
"github.com/openimsdk/tools/utils/stringutil"
|
||
9 months ago
|
"github.com/redis/go-redis/v9"
|
||
|
"golang.org/x/sync/errgroup"
|
||
6 months ago
|
"time"
|
||
1 year ago
|
)
|
||
|
|
||
7 months ago
|
const msgCacheTimeout = 86400 * time.Second
|
||
|
|
||
1 year ago
|
var concurrentLimit = 3
|
||
|
|
||
6 months ago
|
func NewMsgCache(client redis.UniversalClient, redisEnablePipeline bool) cache.MsgCache {
|
||
7 months ago
|
return &msgCache{rdb: client, msgCacheTimeout: msgCacheTimeout, redisEnablePipeline: redisEnablePipeline}
|
||
1 year ago
|
}
|
||
|
|
||
|
type msgCache struct {
|
||
7 months ago
|
rdb redis.UniversalClient
|
||
|
msgCacheTimeout time.Duration
|
||
|
redisEnablePipeline bool
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *msgCache) getAllMessageCacheKey(conversationID string) string {
|
||
|
return cachekey.GetAllMessageCacheKey(conversationID)
|
||
1 year ago
|
}
|
||
|
|
||
7 months ago
|
func (c *msgCache) getMessageCacheKey(conversationID string, seq int64) string {
|
||
6 months ago
|
return cachekey.GetMessageCacheKey(conversationID, seq)
|
||
|
}
|
||
|
func (c *msgCache) getMessageDelUserListKey(conversationID string, seq int64) string {
|
||
|
return cachekey.GetMessageDelUserListKey(conversationID, seq)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) getUserDelList(conversationID, userID string) string {
|
||
|
return cachekey.GetUserDelListKey(conversationID, userID)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) getSendMsgKey(id string) string {
|
||
|
return cachekey.GetSendMsgKey(id)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) getLockMessageTypeKey(clientMsgID string, TypeKey string) string {
|
||
|
return cachekey.GetLockMessageTypeKey(clientMsgID, TypeKey)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||
|
return cachekey.GetMessageReactionExKey(clientMsgID, sessionType)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
|
||
7 months ago
|
if c.redisEnablePipeline {
|
||
1 year ago
|
return c.PipeSetMessageToCache(ctx, conversationID, msgs)
|
||
|
}
|
||
|
return c.ParallelSetMessageToCache(ctx, conversationID, msgs)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) PipeSetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
|
||
|
pipe := c.rdb.Pipeline()
|
||
1 year ago
|
for _, msg := range msgs {
|
||
1 year ago
|
s, err := msgprocessor.Pb2String(msg)
|
||
1 year ago
|
if err != nil {
|
||
9 months ago
|
return 0, err
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
key := c.getMessageCacheKey(conversationID, msg.Seq)
|
||
7 months ago
|
_ = pipe.Set(ctx, key, s, c.msgCacheTimeout)
|
||
1 year ago
|
}
|
||
|
|
||
|
results, err := pipe.Exec(ctx)
|
||
|
if err != nil {
|
||
9 months ago
|
return 0, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
|
||
|
for _, res := range results {
|
||
|
if res.Err() != nil {
|
||
1 year ago
|
return 0, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
return len(msgs), nil
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) ParallelSetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
|
||
|
wg := errgroup.Group{}
|
||
|
wg.SetLimit(concurrentLimit)
|
||
|
|
||
|
for _, msg := range msgs {
|
||
|
msg := msg // closure safe var
|
||
|
wg.Go(func() error {
|
||
|
s, err := msgprocessor.Pb2String(msg)
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
|
||
|
key := c.getMessageCacheKey(conversationID, msg.Seq)
|
||
7 months ago
|
if err := c.rdb.Set(ctx, key, s, c.msgCacheTimeout).Err(); err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
err := wg.Wait()
|
||
|
if err != nil {
|
||
7 months ago
|
return 0, errs.WrapMsg(err, "wg.Wait failed")
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
return len(msgs), nil
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error {
|
||
|
for _, seq := range seqs {
|
||
|
delUserListKey := c.getMessageDelUserListKey(conversationID, seq)
|
||
|
userDelListKey := c.getUserDelList(conversationID, userID)
|
||
1 year ago
|
err := c.rdb.SAdd(ctx, delUserListKey, userID).Err()
|
||
1 year ago
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
1 year ago
|
err = c.rdb.SAdd(ctx, userDelListKey, seq).Err()
|
||
1 year ago
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
7 months ago
|
if err := c.rdb.Expire(ctx, delUserListKey, c.msgCacheTimeout).Err(); err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
7 months ago
|
if err := c.rdb.Expire(ctx, userDelListKey, c.msgCacheTimeout).Err(); err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID string) (seqs []int64, err error) {
|
||
|
result, err := c.rdb.SMembers(ctx, c.getUserDelList(conversationID, userID)).Result()
|
||
|
if err != nil {
|
||
|
return nil, errs.Wrap(err)
|
||
|
}
|
||
|
seqs = make([]int64, len(result))
|
||
|
for i, v := range result {
|
||
7 months ago
|
seqs[i] = stringutil.StringToInt64(v)
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return seqs, nil
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) DelUserDeleteMsgsList(ctx context.Context, conversationID string, seqs []int64) {
|
||
|
for _, seq := range seqs {
|
||
|
delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result()
|
||
|
if err != nil {
|
||
|
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
||
1 year ago
|
|
||
1 year ago
|
continue
|
||
|
}
|
||
|
if len(delUsers) > 0 {
|
||
|
var failedFlag bool
|
||
|
for _, userID := range delUsers {
|
||
1 year ago
|
err = c.rdb.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err()
|
||
1 year ago
|
if err != nil {
|
||
|
failedFlag = true
|
||
1 year ago
|
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq, "userID", userID)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
if !failedFlag {
|
||
1 year ago
|
if err := c.rdb.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil {
|
||
1 year ago
|
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) DeleteMessages(ctx context.Context, conversationID string, seqs []int64) error {
|
||
7 months ago
|
if c.redisEnablePipeline {
|
||
1 year ago
|
return c.PipeDeleteMessages(ctx, conversationID, seqs)
|
||
|
}
|
||
|
|
||
|
return c.ParallelDeleteMessages(ctx, conversationID, seqs)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) ParallelDeleteMessages(ctx context.Context, conversationID string, seqs []int64) error {
|
||
|
wg := errgroup.Group{}
|
||
|
wg.SetLimit(concurrentLimit)
|
||
|
|
||
1 year ago
|
for _, seq := range seqs {
|
||
1 year ago
|
seq := seq
|
||
|
wg.Go(func() error {
|
||
|
err := c.rdb.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return wg.Wait()
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) PipeDeleteMessages(ctx context.Context, conversationID string, seqs []int64) error {
|
||
|
pipe := c.rdb.Pipeline()
|
||
|
for _, seq := range seqs {
|
||
|
_ = pipe.Del(ctx, c.getMessageCacheKey(conversationID, seq))
|
||
|
}
|
||
|
|
||
|
results, err := pipe.Exec(ctx)
|
||
|
if err != nil {
|
||
7 months ago
|
return errs.WrapMsg(err, "pipe.del")
|
||
1 year ago
|
}
|
||
|
|
||
|
for _, res := range results {
|
||
|
if res.Err() != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversationID string) error {
|
||
6 months ago
|
vals, err := c.rdb.Keys(ctx, c.getAllMessageCacheKey(conversationID)).Result()
|
||
12 months ago
|
if errors.Is(err, redis.Nil) {
|
||
|
return nil
|
||
|
}
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
for _, v := range vals {
|
||
|
if err := c.rdb.Del(ctx, v).Err(); err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
}
|
||
12 months ago
|
return nil
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []int64) error {
|
||
|
for _, seq := range seqs {
|
||
|
key := c.getMessageCacheKey(userID, seq)
|
||
|
result, err := c.rdb.Get(ctx, key).Result()
|
||
|
if err != nil {
|
||
1 year ago
|
if errors.Is(err, redis.Nil) {
|
||
1 year ago
|
continue
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
|
var msg sdkws.MsgData
|
||
1 year ago
|
err = jsonpb.UnmarshalString(result, &msg)
|
||
|
if err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
|
msg.Status = constant.MsgDeleted
|
||
1 year ago
|
s, err := msgprocessor.Pb2String(&msg)
|
||
1 year ago
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
7 months ago
|
if err := c.rdb.Set(ctx, key, s, c.msgCacheTimeout).Err(); err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||
6 months ago
|
return errs.Wrap(c.rdb.Set(ctx, c.getSendMsgKey(id), status, time.Hour*24).Err())
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||
6 months ago
|
result, err := c.rdb.Get(ctx, c.getSendMsgKey(id)).Int()
|
||
1 year ago
|
return int32(result), errs.Wrap(err)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||
6 months ago
|
key := c.getLockMessageTypeKey(clientMsgID, TypeKey)
|
||
1 year ago
|
return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||
6 months ago
|
key := c.getLockMessageTypeKey(clientMsgID, TypeKey)
|
||
1 year ago
|
return errs.Wrap(c.rdb.Del(ctx, key).Err())
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||
|
n, err := c.rdb.Exists(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||
|
if err != nil {
|
||
9 months ago
|
return false, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return n > 0, nil
|
||
|
}
|
||
|
|
||
9 months ago
|
func (c *msgCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||
1 year ago
|
return errs.Wrap(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||
|
}
|
||
|
|
||
1 year ago
|
func (c *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||
9 months ago
|
val, err := c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result()
|
||
|
return val, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
func (c *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||
9 months ago
|
val, err := c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result()
|
||
|
return val, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
func (c *msgCache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||
|
val, err := c.rdb.HGetAll(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||
|
return val, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
func (c *msgCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||
1 year ago
|
return errs.Wrap(c.rdb.HDel(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||
|
}
|
||
7 months ago
|
|
||
|
func (c *msgCache) GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
||
|
if c.redisEnablePipeline {
|
||
|
return c.PipeGetMessagesBySeq(ctx, conversationID, seqs)
|
||
|
}
|
||
|
|
||
|
return c.ParallelGetMessagesBySeq(ctx, conversationID, seqs)
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) PipeGetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
||
|
pipe := c.rdb.Pipeline()
|
||
|
|
||
|
results := []*redis.StringCmd{}
|
||
|
for _, seq := range seqs {
|
||
|
results = append(results, pipe.Get(ctx, c.getMessageCacheKey(conversationID, seq)))
|
||
|
}
|
||
|
|
||
|
_, err = pipe.Exec(ctx)
|
||
|
if err != nil && err != redis.Nil {
|
||
|
return seqMsgs, failedSeqs, errs.WrapMsg(err, "pipe.get")
|
||
|
}
|
||
|
|
||
|
for idx, res := range results {
|
||
|
seq := seqs[idx]
|
||
|
if res.Err() != nil {
|
||
|
log.ZError(ctx, "GetMessagesBySeq failed", err, "conversationID", conversationID, "seq", seq, "err", res.Err())
|
||
|
failedSeqs = append(failedSeqs, seq)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
msg := sdkws.MsgData{}
|
||
|
if err = msgprocessor.String2Pb(res.Val(), &msg); err != nil {
|
||
|
log.ZError(ctx, "GetMessagesBySeq Unmarshal failed", err, "res", res, "conversationID", conversationID, "seq", seq)
|
||
|
failedSeqs = append(failedSeqs, seq)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if msg.Status == constant.MsgDeleted {
|
||
|
failedSeqs = append(failedSeqs, seq)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
seqMsgs = append(seqMsgs, &msg)
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (c *msgCache) ParallelGetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
||
|
type entry struct {
|
||
|
err error
|
||
|
msg *sdkws.MsgData
|
||
|
}
|
||
|
|
||
|
wg := errgroup.Group{}
|
||
|
wg.SetLimit(concurrentLimit)
|
||
|
|
||
|
results := make([]entry, len(seqs)) // set slice len/cap to length of seqs.
|
||
|
for idx, seq := range seqs {
|
||
|
// closure safe var
|
||
|
idx := idx
|
||
|
seq := seq
|
||
|
|
||
|
wg.Go(func() error {
|
||
|
res, err := c.rdb.Get(ctx, c.getMessageCacheKey(conversationID, seq)).Result()
|
||
|
if err != nil {
|
||
|
log.ZError(ctx, "GetMessagesBySeq failed", err, "conversationID", conversationID, "seq", seq)
|
||
|
results[idx] = entry{err: err}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
msg := sdkws.MsgData{}
|
||
|
if err = msgprocessor.String2Pb(res, &msg); err != nil {
|
||
|
log.ZError(ctx, "GetMessagesBySeq Unmarshal failed", err, "res", res, "conversationID", conversationID, "seq", seq)
|
||
|
results[idx] = entry{err: err}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
if msg.Status == constant.MsgDeleted {
|
||
|
results[idx] = entry{err: err}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
results[idx] = entry{msg: &msg}
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
_ = wg.Wait()
|
||
|
|
||
|
for idx, res := range results {
|
||
|
if res.err != nil {
|
||
|
failedSeqs = append(failedSeqs, seqs[idx])
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
seqMsgs = append(seqMsgs, res.msg)
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|