userRoomVisit.go 4.29 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6
package room_c

import (
	"context"
	"fmt"
	"git.hilo.cn/hilo-common/mylogrus"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/resource/config"
hujiebin's avatar
hujiebin committed
8
	"git.hilo.cn/hilo-common/resource/redisCli"
9
	"github.com/bluele/gcache"
hujiebin's avatar
hujiebin committed
10
	redis2 "github.com/go-redis/redis/v8"
11
	"github.com/spf13/cast"
hujiebin's avatar
hujiebin committed
12
	"hilo-group/_const/redis_key"
13
	"hilo-group/_const/redis_key/group_k"
hujiebin's avatar
hujiebin committed
14 15 16
	"time"
)

hujiebin's avatar
hujiebin committed
17 18
// 处理访问房间的相关缓存
func ProcessRoomVisit(groupId string, userId uint64) error {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	//key := redis_key.GetPrefixGroupInUserDuration(groupId)
	//now := time.Now()
	//ret, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{
	//	Score:  float64(now.Unix()),
	//	Member: userId,
	//}).Result()
	//if err != nil {
	//	return err
	//}
	//mylogrus.MyLog.Infof("ProcessRoomVisit, ZADD %s, return %d", key, ret)
	//
	//// 每群定时清一次数据可以了
	//if now.Second()%redis_key.GroupInDurationClearPeriod == 0 {
	//	rc, err := clearRoomVisit(groupId, now.AddDate(0, 0, -redis_key.GroupInDurationTTL))
	//	if err == nil {
	//		mylogrus.MyLog.Infof("ProcessRoomVisit, clearRoomVisit %s, return %d", key, rc)
	//	} else {
	//		mylogrus.MyLog.Warnf("ProcessRoomVisit, clearRoomVisit %s, failed %s", key, err.Error())
	//	}
	//}
hujiebin's avatar
hujiebin committed
39 40 41

	// 马上更新roomVisitCount
	if _, err := GetSetRoomVisitCount(groupId); err != nil {
42
		mylogrus.MyLog.Errorf("ProcessRoomVisit, failed for groupId %s, err: %s", groupId, err.Error())
hujiebin's avatar
hujiebin committed
43 44 45 46 47
	}

	return nil
}

hujiebin's avatar
hujiebin committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
func ProcessUserRoomVisit(userId uint64, groupId string) error {
	key := redis_key.GetUserEnterRoomKey(userId)
	now := time.Now()
	ret, err := redisCli.GetRedis().ZAdd(context.Background(), key, &redis2.Z{
		Score:  float64(now.Unix()),
		Member: groupId,
	}).Result()
	if err != nil {
		return err
	}
	mylogrus.MyLog.Infof("ProcessUserRoomVisit, ZADD %s, return %d", key, ret)

	return nil
}

// 查询用户访问过的房间及其时间
hujiebin's avatar
hujiebin committed
64
// todo 去掉查询,redis性能
hujiebin's avatar
hujiebin committed
65
func GetUserRoomVisit(userId uint64) (map[string]int64, error) {
hujiebin's avatar
hujiebin committed
66 67
	result := make(map[string]int64, 0)
	return result, nil
hujiebin's avatar
hujiebin committed
68 69 70 71 72
	key := redis_key.GetUserEnterRoomKey(userId)
	ret, err := redisCli.GetRedis().ZRangeWithScores(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, err
	}
hujiebin's avatar
hujiebin committed
73
	//mylogrus.MyLog.Infof("GetUserRoomVisit, ZRangeWithScores %s, return %v", key, ret)
hujiebin's avatar
hujiebin committed
74 75 76 77 78 79 80 81

	for _, i := range ret {
		result[i.Member.(string)] = int64(i.Score)
	}
	return result, nil
}

// 批量获取房间访问人数
82
// 带上lru
83 84
var roomVisitCountLru = gcache.New(100).LRU().Build()

hujiebin's avatar
hujiebin committed
85 86 87 88 89
func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
	visit := make(map[string]string)
	if len(groupIds) <= 0 {
		return visit, nil
	}
90 91 92 93 94
	lKey := "room:visit:count"
	if data, err := roomVisitCountLru.Get(lKey); err == nil {
		return data.(map[string]string), nil
	}
	// 用redisCluster中的zset代替,只是取出大于10的
95
	zKey := redis_key.GetPrefixRoomVisitCountZset()
96
	cnt := 10
hujiebin's avatar
hujiebin committed
97 98 99
	if !config.AppIsRelease() {
		cnt = 0
	}
100
	zRes, err := redisCli.GetClusterRedis().ZRevRangeByScoreWithScores(context.Background(), zKey, &redis2.ZRangeBy{
101
		Min: fmt.Sprintf("%d", cnt),
102 103 104 105 106 107 108 109 110
		Max: "+inf",
	}).Result()
	if err != nil {
		mylogrus.MyLog.Errorf("MGetRoomVisitCount zset fail:%v", err)
		return visit, err
	}
	for _, v := range zRes {
		visit[cast.ToString(v.Member)] = cast.ToString(v.Score)
	}
111
	_ = roomVisitCountLru.SetWithExpire(lKey, visit, time.Minute*15)
112
	return visit, nil
hujiebin's avatar
hujiebin committed
113 114 115
}

func GetSetRoomVisitCount(groupId string) (int64, error) {
116 117
	//key := redis_key.GetPrefixGroupInUserDuration(groupId)
	key := group_k.GetUserEnterRoomGroupKey(groupId)
hujiebin's avatar
hujiebin committed
118 119 120
	vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result()
	// 查到合法值后,更新二级缓存
	if err == nil && vc >= 0 {
hujiebin's avatar
hujiebin committed
121 122
		if err := saveRoomVisitCount(groupId, vc); err != nil {
			mylogrus.MyLog.Errorf("saveRoomVisitCount %s, err: %v", groupId, err)
123
		}
hujiebin's avatar
hujiebin committed
124 125 126 127
	}
	return vc, err
}

hujiebin's avatar
hujiebin committed
128 129
func saveRoomVisitCount(groupId string, count int64) error {
	//key := redis_key.GetPrefixRoomVisitCount()
130 131 132 133 134 135 136
	zKey := redis_key.GetPrefixRoomVisitCountZset()
	// zset同步存一份到redis集群
	if err := redisCli.GetClusterRedis().ZAdd(context.Background(), zKey, &redis2.Z{
		Score:  float64(count),
		Member: groupId,
	}).Err(); err != nil {
		mylogrus.MyLog.Errorf("GetPrefixRoomVisitCountZset fail %s", err.Error())
hujiebin's avatar
hujiebin committed
137
		return err
138
	}
hujiebin's avatar
hujiebin committed
139 140
	return nil
	//return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result()
hujiebin's avatar
hujiebin committed
141
}