From 298be3ebcceb2756316c32f23d832d0a988ed8c3 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 25 Jul 2023 17:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=BF=9B=E6=88=BF=E7=9A=84ke?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _const/redis_key/group_k/enter_room.go | 21 +++++++++ domain/cache/group_c/enter_room.go | 61 ++++++++++++++++++++++++++ domain/service/event_s/event_init.go | 2 + 3 files changed, 84 insertions(+) create mode 100644 _const/redis_key/group_k/enter_room.go create mode 100644 domain/cache/group_c/enter_room.go diff --git a/_const/redis_key/group_k/enter_room.go b/_const/redis_key/group_k/enter_room.go new file mode 100644 index 0000000..3e01264 --- /dev/null +++ b/_const/redis_key/group_k/enter_room.go @@ -0,0 +1,21 @@ +package group_k + +import ( + "fmt" + "hilo-group/_const/redis_key" +) + +// 用户进房的key +const ( + UserEnterRoomPrefix = "uer:" + UserEnterRoomUserKey = UserEnterRoomPrefix + "u:${user_id}" + UserEnterRoomGroupKey = UserEnterRoomPrefix + "g:${user_id}" +) + +func GetUserEnterRoomUserKey(userId uint64) string { + return redis_key.ReplaceKey(UserEnterRoomUserKey, fmt.Sprintf("%d", userId)) +} + +func GetUserEnterRoomGroupKey(groupId string) string { + return redis_key.ReplaceKey(UserEnterRoomGroupKey, groupId) +} diff --git a/domain/cache/group_c/enter_room.go b/domain/cache/group_c/enter_room.go new file mode 100644 index 0000000..a164a04 --- /dev/null +++ b/domain/cache/group_c/enter_room.go @@ -0,0 +1,61 @@ +package group_c + +import ( + "fmt" + "git.hilo.cn/hilo-common/domain" + "github.com/go-redis/redis/v8" + "hilo-group/_const/redis_key/group_k" + "strconv" + "time" +) + +// 用户进房 +func ZAddUserEnterRoom(model *domain.Model, userId uint64, imGroupId string) { + userKey := group_k.GetUserEnterRoomUserKey(userId) + groupKey := group_k.GetUserEnterRoomGroupKey(imGroupId) + if err := model.Redis.ZAdd(model, userKey, &redis.Z{ + Score: float64(time.Now().Unix()), + Member: imGroupId, + }).Err(); err != nil { + model.Log.Errorf("ZAddUserEnterRoom user fail:%v", err) + } + if err := model.Redis.ZAdd(model, groupKey, &redis.Z{ + Score: float64(time.Now().Unix()), + Member: userId, + }).Err(); err != nil { + model.Log.Errorf("ZAddUserEnterRoom room fail:%v", err) + } +} + +// 获取最近房间访客 +func GetLastRoomVisitors(model *domain.Model, imGroupId string) (userIds []uint64) { + groupKey := group_k.GetUserEnterRoomGroupKey(imGroupId) + res, err := model.Redis.ZRangeByScore(model, groupKey, &redis.ZRangeBy{ + Min: fmt.Sprintf("%d", time.Now().AddDate(0, 0, -15).Unix()), + Max: "+inf", + }).Result() + if err != nil { + return + } + for _, u := range res { + uid, _ := strconv.ParseUint(u, 10, 64) + if uid > 0 { + userIds = append(userIds) + } + } + return +} + +// 获取最近进入的房间 +func GetUserRecentRooms(model *domain.Model, userId uint64) (imGroupIds []string) { + userKey := group_k.GetUserEnterRoomUserKey(userId) + var err error + imGroupIds, err = model.Redis.ZRangeByScore(model, userKey, &redis.ZRangeBy{ + Min: fmt.Sprintf("%d", time.Now().AddDate(0, 0, -15).Unix()), + Max: "+inf", + }).Result() + if err != nil { + return + } + return +} diff --git a/domain/service/event_s/event_init.go b/domain/service/event_s/event_init.go index dfb6406..80447d9 100644 --- a/domain/service/event_s/event_init.go +++ b/domain/service/event_s/event_init.go @@ -10,6 +10,7 @@ import ( "hilo-group/_const/enum/group_e" "hilo-group/_const/enum/msg_e" "hilo-group/_const/enum/task_e" + "hilo-group/domain/cache/group_c" "hilo-group/domain/event/gift_ev" "hilo-group/domain/event/group_ev" "hilo-group/domain/event/group_power_ev" @@ -246,6 +247,7 @@ func GroupEvents() { } err := uer.Save(model.Db) model.Log.Infof("AddGroupInAsync, UserEnterRoom err: %v", err) + group_c.ZAddUserEnterRoom(model, event.UserId, event.GroupId) // redis存储 return err }) -- 2.22.0