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

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

hujiebin's avatar
hujiebin committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// 处理访问房间的相关缓存
func ProcessRoomVisit(groupId string, userId uint64) error {
	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())
		}
	}

	// 马上更新roomVisitCount
	if _, err := GetSetRoomVisitCount(groupId); err != nil {
		mylogrus.MyLog.Warnf("ProcessRoomVisit, failed for key %s, err: %s", key, err.Error())
	}

	return nil
}

hujiebin's avatar
hujiebin committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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
63
// todo 去掉查询,redis性能
hujiebin's avatar
hujiebin committed
64
func GetUserRoomVisit(userId uint64) (map[string]int64, error) {
hujiebin's avatar
hujiebin committed
65 66
	result := make(map[string]int64, 0)
	return result, nil
hujiebin's avatar
hujiebin committed
67 68 69 70 71
	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
72
	//mylogrus.MyLog.Infof("GetUserRoomVisit, ZRangeWithScores %s, return %v", key, ret)
hujiebin's avatar
hujiebin committed
73 74 75 76 77 78 79

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

80 81
var roomVisitCntCache = gcache.New(50000).LRU().Build()

hujiebin's avatar
hujiebin committed
82
// 批量获取房间访问人数
83
// 带上lru
hujiebin's avatar
hujiebin committed
84 85 86 87 88
func MGetRoomVisitCount(groupIds []string) (map[string]string, error) {
	visit := make(map[string]string)
	if len(groupIds) <= 0 {
		return visit, nil
	}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

	var miss []string
	for _, groupId := range groupIds {
		if data, err := roomVisitCntCache.Get(groupId); err == nil {
			visit[groupId] = data.(string)
		} else {
			miss = append(miss, groupId)
		}
	}
	if len(miss) <= 0 {
		return visit, nil
	}

	key := redis_key.GetPrefixRoomVisitCount()
	res, err := redisCli.GetRedis().HMGet(context.Background(), key, miss...).Result()
hujiebin's avatar
hujiebin committed
104 105 106
	if err != nil {
		return visit, err
	}
107 108
	if len(res) != len(miss) {
		return visit, errors.New(fmt.Sprintf("MGetRoomVisitCount fail,miss match len,%v-%v", len(res), len(miss)))
hujiebin's avatar
hujiebin committed
109
	}
110
	for i, groupId := range miss {
hujiebin's avatar
hujiebin committed
111 112
		if cnt, ok := res[i].(string); ok {
			visit[groupId] = cnt
113
			_ = roomVisitCntCache.SetWithExpire(groupId, cnt, time.Minute*15)
hujiebin's avatar
hujiebin committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		}
	}
	return visit, nil
}

func GetSetRoomVisitCount(groupId string) (int64, error) {
	key := redis_key.GetPrefixGroupInUserDuration(groupId)
	vc, err := redisCli.GetRedis().ZCard(context.Background(), key).Result()
	// 查到合法值后,更新二级缓存
	if err == nil && vc >= 0 {
		ret, err := saveRoomVisitCount(groupId, vc)
		mylogrus.MyLog.Infof("saveRoomVisitCount %s, ret = %d, err: %v", groupId, ret, err)
	}
	return vc, err
}

func saveRoomVisitCount(groupId string, count int64) (int64, error) {
	key := redis_key.GetPrefixRoomVisitCount()
	return redisCli.GetRedis().HSet(context.Background(), key, groupId, strconv.FormatInt(count, 10)).Result()
}
hujiebin's avatar
hujiebin committed
134 135 136 137 138 139 140 141 142 143

func clearRoomVisit(groupId string, t time.Time) (int64, error) {
	value := strconv.FormatInt(t.Unix(), 10)
	ret, err := redisCli.GetRedis().ZRemRangeByScore(context.Background(), redis_key.GetPrefixGroupInUserDuration(groupId), "0", value).Result()
	if err != nil {
		return 0, err
	}
	return ret, nil
}

JiebinHu's avatar
JiebinHu committed
144
// Deprecated: hgetall有问题
hujiebin's avatar
hujiebin committed
145 146 147 148
func GetAllRoomVisitCount() (map[string]string, error) {
	key := redis_key.GetPrefixRoomVisitCount()
	return redisCli.GetRedis().HGetAll(context.Background(), key).Result()
}