mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-31 16:32:12 +08:00 
			
		
		
		
	* fix: GroupApplicationAcceptedNotification * fix: GroupApplicationAcceptedNotification * fix: NotificationUserInfoUpdate * cicd: robot automated Change * fix: component * fix: getConversationInfo * feat: cron task * feat: cron task * feat: cron task * feat: cron task * feat: cron task * fix: minio config url recognition error * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * new mongo * friend incr sync * friend incr sync * friend incr sync * friend incr sync * friend incr sync * mage * optimization version log * optimization version log * sync * sync * sync * group sync * sync option * sync option * refactor: replace `friend` package with `realtion`. * refactor: update lastest commit to relation. * sync option * sync option * sync option * sync * sync * go.mod * seq * update: go mod * refactor: change incremental to full * feat: get full friend user ids * feat: api and config * seq * group version * merge * seq * seq * seq * fix: sort by id avoid unstable sort friends. * group * group * group * fix: sort by id avoid unstable sort friends. * fix: sort by id avoid unstable sort friends. * fix: sort by id avoid unstable sort friends. * user version * seq * seq * seq user * user online * implement minio expire delete. * user online * config * fix * fix * implement minio expire delete logic. * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * online cache * feat: implement scheduled delete outdated object in minio. * update gomake version * update gomake version * implement FindExpires pagination. * remove unnesseary incr. * fix uncorrect args call. * online push * online push * online push * resolving conflicts * resolving conflicts * test * api prommetrics * api prommetrics * api prommetrics * api prommetrics * api prommetrics * rpc prommetrics * rpc prommetrics * online status * online status * online status * online status * sub * conversation version incremental * merge seq * merge online * merge online * merge online * merge seq * GetOwnerConversation * fix: change incremental syncer router name. * rockscache batch get * rockscache seq batch get * fix: GetMsgDocModelByIndex bug * update go.mod * update go.mod * merge * feat: prometheus * feat: prometheus --------- Co-authored-by: withchao <withchao@users.noreply.github.com> Co-authored-by: Monet Lee <monet_lee@163.com> Co-authored-by: OpenIM-Gordon <46924906+FGadvancer@users.noreply.github.com> Co-authored-by: icey-yu <1186114839@qq.com>
		
			
				
	
	
		
			187 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			187 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // 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.
 | |
| 
 | |
| package redis
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
 | |
| 	"github.com/openimsdk/protocol/sdkws"
 | |
| 	"github.com/openimsdk/tools/errs"
 | |
| 	"github.com/openimsdk/tools/utils/datautil"
 | |
| 	"github.com/redis/go-redis/v9"
 | |
| 	"time"
 | |
| ) //
 | |
| 
 | |
| // msgCacheTimeout is  expiration time of message cache, 86400 seconds
 | |
| const msgCacheTimeout = 86400
 | |
| 
 | |
| func NewMsgCache(client redis.UniversalClient) cache.MsgCache {
 | |
| 	return &msgCache{rdb: client}
 | |
| }
 | |
| 
 | |
| type msgCache struct {
 | |
| 	rdb redis.UniversalClient
 | |
| }
 | |
| 
 | |
| func (c *msgCache) getMessageCacheKey(conversationID string, seq int64) string {
 | |
| 	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)
 | |
| }
 | |
| 
 | |
| func (c *msgCache) SetMessagesToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
 | |
| 	msgMap := datautil.SliceToMap(msgs, func(msg *sdkws.MsgData) string {
 | |
| 		return c.getMessageCacheKey(conversationID, msg.Seq)
 | |
| 	})
 | |
| 	keys := datautil.Slice(msgs, func(msg *sdkws.MsgData) string {
 | |
| 		return c.getMessageCacheKey(conversationID, msg.Seq)
 | |
| 	})
 | |
| 	err := ProcessKeysBySlot(ctx, c.rdb, keys, func(ctx context.Context, slot int64, keys []string) error {
 | |
| 		var values []string
 | |
| 		for _, key := range keys {
 | |
| 			if msg, ok := msgMap[key]; ok {
 | |
| 				s, err := msgprocessor.Pb2String(msg)
 | |
| 				if err != nil {
 | |
| 					return err
 | |
| 				}
 | |
| 				values = append(values, s)
 | |
| 			}
 | |
| 		}
 | |
| 		return LuaSetBatchWithCommonExpire(ctx, c.rdb, keys, values, msgCacheTimeout)
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 	return len(msgs), nil
 | |
| }
 | |
| 
 | |
| func (c *msgCache) DeleteMessagesFromCache(ctx context.Context, conversationID string, seqs []int64) error {
 | |
| 	var keys []string
 | |
| 	for _, seq := range seqs {
 | |
| 		keys = append(keys, c.getMessageCacheKey(conversationID, seq))
 | |
| 	}
 | |
| 
 | |
| 	return ProcessKeysBySlot(ctx, c.rdb, keys, func(ctx context.Context, slot int64, keys []string) error {
 | |
| 		return LuaDeleteBatch(ctx, c.rdb, keys)
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func (c *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
 | |
| 	return errs.Wrap(c.rdb.Set(ctx, c.getSendMsgKey(id), status, time.Hour*24).Err())
 | |
| }
 | |
| 
 | |
| func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
 | |
| 	result, err := c.rdb.Get(ctx, c.getSendMsgKey(id)).Int()
 | |
| 	return int32(result), errs.Wrap(err)
 | |
| }
 | |
| 
 | |
| func (c *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
 | |
| 	key := c.getLockMessageTypeKey(clientMsgID, TypeKey)
 | |
| 	return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
 | |
| }
 | |
| 
 | |
| func (c *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
 | |
| 	key := c.getLockMessageTypeKey(clientMsgID, TypeKey)
 | |
| 	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 {
 | |
| 		return false, errs.Wrap(err)
 | |
| 	}
 | |
| 
 | |
| 	return n > 0, nil
 | |
| }
 | |
| 
 | |
| func (c *msgCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
 | |
| 	return errs.Wrap(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
 | |
| }
 | |
| 
 | |
| func (c *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
 | |
| 	val, err := c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result()
 | |
| 	return val, errs.Wrap(err)
 | |
| }
 | |
| 
 | |
| func (c *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
 | |
| 	val, err := c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result()
 | |
| 	return val, errs.Wrap(err)
 | |
| }
 | |
| 
 | |
| 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)
 | |
| }
 | |
| 
 | |
| func (c *msgCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
 | |
| 	return errs.Wrap(c.rdb.HDel(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
 | |
| }
 | |
| 
 | |
| func (c *msgCache) GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
 | |
| 	var keys []string
 | |
| 	keySeqMap := make(map[string]int64, 10)
 | |
| 	for _, seq := range seqs {
 | |
| 		key := c.getMessageCacheKey(conversationID, seq)
 | |
| 		keys = append(keys, key)
 | |
| 		keySeqMap[key] = seq
 | |
| 	}
 | |
| 	err = ProcessKeysBySlot(ctx, c.rdb, keys, func(ctx context.Context, slot int64, keys []string) error {
 | |
| 		result, err := LuaGetBatch(ctx, c.rdb, keys)
 | |
| 		if err != nil {
 | |
| 			return err
 | |
| 		}
 | |
| 		for i, value := range result {
 | |
| 			seq := keySeqMap[keys[i]]
 | |
| 			if value == nil {
 | |
| 				failedSeqs = append(failedSeqs, seq)
 | |
| 				continue
 | |
| 			}
 | |
| 
 | |
| 			msg := &sdkws.MsgData{}
 | |
| 			msgString, ok := value.(string)
 | |
| 			if !ok || msgprocessor.String2Pb(msgString, msg) != nil {
 | |
| 				failedSeqs = append(failedSeqs, seq)
 | |
| 				continue
 | |
| 			}
 | |
| 			seqMsgs = append(seqMsgs, msg)
 | |
| 
 | |
| 		}
 | |
| 		return nil
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		return nil, nil, err
 | |
| 	}
 | |
| 	return seqMsgs, failedSeqs, nil
 | |
| }
 |