mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-31 08:29:33 +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 * group member sort * sub * sub * fix: seq conversion bug * fix: redis pipe exec * sort version * sort version * sort version * remove old version online subscription * remove old version online subscription * version log index * version log index * batch push * batch push * seq void filling * fix: batchGetMaxSeq * fix: batchGetMaxSeq * cache db error log * 111 * fix bug * fix: ImportFriends * add online cache * add some logs * add some logs * fix: onlineUserIDs * add logs * test * test * test * test * add log * feat: solve the problem that modifying the cached data affects other * feat: solve the problem that modifying the cached data affects other * feat: search messages to filter out notifications --------- 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>
		
			
				
	
	
		
			117 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package rpccache
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/localcache"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/localcache/lru"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/util/useronline"
 | |
| 	"github.com/openimsdk/tools/log"
 | |
| 	"github.com/openimsdk/tools/mcontext"
 | |
| 	"github.com/redis/go-redis/v9"
 | |
| 	"math/rand"
 | |
| 	"strconv"
 | |
| 	"time"
 | |
| )
 | |
| 
 | |
| func NewOnlineCache(user rpcclient.UserRpcClient, group *GroupLocalCache, rdb redis.UniversalClient, fn func(ctx context.Context, userID string, platformIDs []int32)) *OnlineCache {
 | |
| 	x := &OnlineCache{
 | |
| 		user:  user,
 | |
| 		group: group,
 | |
| 		local: lru.NewSlotLRU(1024, localcache.LRUStringHash, func() lru.LRU[string, []int32] {
 | |
| 			return lru.NewLayLRU[string, []int32](2048, cachekey.OnlineExpire/2, time.Second*3, localcache.EmptyTarget{}, func(key string, value []int32) {})
 | |
| 		}),
 | |
| 	}
 | |
| 	go func() {
 | |
| 		ctx := mcontext.SetOperationID(context.Background(), cachekey.OnlineChannel+strconv.FormatUint(rand.Uint64(), 10))
 | |
| 		for message := range rdb.Subscribe(ctx, cachekey.OnlineChannel).Channel() {
 | |
| 			userID, platformIDs, err := useronline.ParseUserOnlineStatus(message.Payload)
 | |
| 			if err != nil {
 | |
| 				log.ZError(ctx, "OnlineCache setUserOnline redis subscribe parseUserOnlineStatus", err, "payload", message.Payload, "channel", message.Channel)
 | |
| 				continue
 | |
| 			}
 | |
| 			storageCache := x.setUserOnline(userID, platformIDs)
 | |
| 			log.ZDebug(ctx, "OnlineCache setUserOnline", "userID", userID, "platformIDs", platformIDs, "payload", message.Payload, "storageCache", storageCache)
 | |
| 			if fn != nil {
 | |
| 				fn(ctx, userID, platformIDs)
 | |
| 			}
 | |
| 		}
 | |
| 	}()
 | |
| 	return x
 | |
| }
 | |
| 
 | |
| type OnlineCache struct {
 | |
| 	user  rpcclient.UserRpcClient
 | |
| 	group *GroupLocalCache
 | |
| 	local lru.LRU[string, []int32]
 | |
| }
 | |
| 
 | |
| func (o *OnlineCache) getUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
 | |
| 	platformIDs, err := o.local.Get(userID, func() ([]int32, error) {
 | |
| 		return o.user.GetUserOnlinePlatform(ctx, userID)
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		log.ZError(ctx, "OnlineCache GetUserOnlinePlatform", err, "userID", userID)
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	log.ZDebug(ctx, "OnlineCache GetUserOnlinePlatform", "userID", userID, "platformIDs", platformIDs)
 | |
| 	return platformIDs, nil
 | |
| }
 | |
| 
 | |
| func (o *OnlineCache) GetUserOnlinePlatform(ctx context.Context, userID string) ([]int32, error) {
 | |
| 	platformIDs, err := o.getUserOnlinePlatform(ctx, userID)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	tmp := make([]int32, len(platformIDs))
 | |
| 	copy(tmp, platformIDs)
 | |
| 	return platformIDs, nil
 | |
| }
 | |
| 
 | |
| func (o *OnlineCache) GetUserOnline(ctx context.Context, userID string) (bool, error) {
 | |
| 	platformIDs, err := o.getUserOnlinePlatform(ctx, userID)
 | |
| 	if err != nil {
 | |
| 		return false, err
 | |
| 	}
 | |
| 	return len(platformIDs) > 0, nil
 | |
| }
 | |
| 
 | |
| //func (o *OnlineCache) GetUsersOnline(ctx context.Context, userIDs []string) ([]string, error) {
 | |
| //	onlineUserIDs := make([]string, 0, len(userIDs))
 | |
| //	for _, userID := range userIDs {
 | |
| //		online, err := o.GetUserOnline(ctx, userID)
 | |
| //		if err != nil {
 | |
| //			return nil, err
 | |
| //		}
 | |
| //		if online {
 | |
| //			onlineUserIDs = append(onlineUserIDs, userID)
 | |
| //		}
 | |
| //	}
 | |
| //	log.ZDebug(ctx, "OnlineCache GetUsersOnline", "userIDs", userIDs, "onlineUserIDs", onlineUserIDs)
 | |
| //	return onlineUserIDs, nil
 | |
| //}
 | |
| //
 | |
| //func (o *OnlineCache) GetGroupOnline(ctx context.Context, groupID string) ([]string, error) {
 | |
| //	userIDs, err := o.group.GetGroupMemberIDs(ctx, groupID)
 | |
| //	if err != nil {
 | |
| //		return nil, err
 | |
| //	}
 | |
| //	var onlineUserIDs []string
 | |
| //	for _, userID := range userIDs {
 | |
| //		online, err := o.GetUserOnline(ctx, userID)
 | |
| //		if err != nil {
 | |
| //			return nil, err
 | |
| //		}
 | |
| //		if online {
 | |
| //			onlineUserIDs = append(onlineUserIDs, userID)
 | |
| //		}
 | |
| //	}
 | |
| //	log.ZDebug(ctx, "OnlineCache GetGroupOnline", "groupID", groupID, "onlineUserIDs", onlineUserIDs, "allUserID", userIDs)
 | |
| //	return onlineUserIDs, nil
 | |
| //}
 | |
| 
 | |
| func (o *OnlineCache) setUserOnline(userID string, platformIDs []int32) bool {
 | |
| 	return o.local.SetHas(userID, platformIDs)
 | |
| }
 |