redis.go 7.73 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
package algo

import (
	"context"
	"encoding/json"
	"github.com/go-redis/redis/v8"
	"hilo-algoCenter/common/config"
	"hilo-algoCenter/common/mylogrus"
	"hilo-algoCenter/common/redisCli"
	"strconv"
	"strings"
	"time"
)

type RecommendUserCache struct {
	UserId     uint64
	Sex        uint8
	Country    string
	ExternalId string
}

// MatchCycle 匹配周期
type MatchCycle struct {
	cycle string
}

type MatchTradeUnionCache struct {
	UserId     uint64
	Sex        uint8
	Country    string
	ExternalId string
}

// MatchUserCache 用户信息
type MatchUserCache struct {
	UserId     uint64
	Sex        uint8
	Country    string
	ExternalId string
	IsVip      bool
}

// MatchRelation 用户关系分数
type MatchRelation struct {
	UserId         uint64
	RelationUserId uint64
	Score          float64
}

func GetMatchCycle() int64 {
	return time.Now().Unix() / int64(config.GetMatchConfig().MATCH_CYCLE)
}

func SetCycle(cycle int64) (bool, error) {
	return redisCli.RedisClient.SetNX(context.Background(), GetPrefixMatchCycle(cycle), cycle, time.Hour).Result()
}

// InitMatchCycle 初始化匹配周期
func InitMatchCycle(cycle int64) *MatchCycle {
	return &MatchCycle{cycle: strconv.Itoa(int(cycle))}
}

func (matchCycle *MatchCycle) GetCycle() string {
	return matchCycle.cycle
}

func (matchCycle *MatchCycle) GetUser(userIds []string) (map[string]MatchUserCache, error) {
	key := GetPrefixMatchUser(matchCycle.cycle)
	dList, err := redisCli.RedisClient.HMGet(context.Background(), key, userIds...).Result()
	if err != nil {
		return nil, err
	}
	userCacheMap := map[string]MatchUserCache{}
	for _, v := range dList {
		//转换成json
		matchUserCache := MatchUserCache{}
		if err := json.Unmarshal([]byte(v.(string)), &matchUserCache); err != nil {
			mylogrus.MyLog.Errorf("match cache user 转换成结构失败,string:%v, 错误:%v", v.(string), err)
		}
		userCacheMap[strconv.Itoa(int(matchUserCache.UserId))] = matchUserCache
	}
	return userCacheMap, nil
}

// GetExcellentData 获取某个周期下,获取所有的质量分数
func (matchCycle *MatchCycle) GetExcellentData() ([]redis.Z, error) {
	key := GetPrefixMatchExcellent(matchCycle.cycle)
	zList, err := redisCli.RedisClient.ZRangeWithScores(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, err
	}
	return zList, nil
}

// GetPriorityData 获取某个周期下,获取的排序数据
func (matchCycle *MatchCycle) GetPriorityData() ([]redis.Z, error) {
	key := GetPrefixMatchPriority(matchCycle.cycle)
	zList, err := redisCli.RedisClient.ZRangeWithScores(context.Background(), key, 0, -1).Result()
	if err != nil {
		return nil, err
	}
	return zList, nil
}

// GetRelationData 获取关系分数
func (matchCycle *MatchCycle) GetRelationData(userId string) ([]MatchRelation, error) {
	key := GetPrefixMatchRelation(matchCycle.cycle, userId)
	values, err := redisCli.RedisClient.HVals(context.Background(), key).Result()
	if err != nil {
		return nil, err
	}
	var matchRelations []MatchRelation
	for _, v := range values {
		var matchRelation MatchRelation
		if err := json.Unmarshal([]byte(v), &matchRelation); err != nil {
			mylogrus.MyLog.Errorf("match cache matchRelations 转换成结构失败,string:%v, 错误:%v", v, err)
		}
		matchRelations = append(matchRelations, matchRelation)
	}
	return matchRelations, nil
}

//匹配第一帧
const matchCallReady = "match_call_ready_{matchUid}"

func GetPrefixMatchCycle(cycle int64) string {
	return strings.Replace(matchCallReady, "{matchUid}", strconv.Itoa(int(cycle)), -1)
}

//推荐用户, 记录起来,用户算法中心获取。
const recommendUser = "recommend_user"

func GetPreRecommendUser() string {
	return recommendUser
}

//匹配的工会用户
const matchTradeUnion = "match_trade_union_{version}"

func GetPreMatchTradeUnion(version string) string {
	return strings.Replace(matchTradeUnion, "{version}", version, -1)
}

//匹配,用户信息
const matchUserKey = "match_user_{version}"

func GetPrefixMatchUser(version string) string {
	return strings.Replace(matchUserKey, "{version}", version, -1)
}

//匹配,质量分数
const matchExcellent = "match_excellent_{version}"

func GetPrefixMatchExcellent(version string) string {
	return strings.Replace(matchExcellent, "{version}", version, -1)
}

//匹配,用户优先度排序
const matchPriority = "match_priority_{version}"

func GetPrefixMatchPriority(version string) string {
	return strings.Replace(matchPriority, "{version}", version, -1)
}

//匹配,关系分数
const matchRelation = "match_relation_{version}_{userId}"

func GetPrefixMatchRelation(version string, userId string) string {
	return strings.Replace(strings.Replace(matchRelation, "{version}", version, -1), "{userId}", userId, -1)
}

//匹配,拉黑名单
const matchBlock = "match_block_{version}"

func GetPrefixMatchBlock(version string) string {
	return strings.Replace(matchBlock, "{version}", version, -1)
}

//匹配,条件性别
const matchConditionSex = "match_condition_sex_{version}"

func GetPrefixMatchConditionSex(version string) string {
	return strings.Replace(matchConditionSex, "{version}", version, -1)
}

//匹配,条件国家
const matchConditionCountry = "match_condition_country_{version}"

func GetPrefixMatchConditionCountry(version string) string {
	return strings.Replace(matchConditionCountry, "{version}", version, -1)
}

// GetRecommendCache 获取推荐人信息
func GetRecommendCache() ([]RecommendUserCache, error) {
	//获取推荐人信息缓存
	key := GetPreRecommendUser()
	strs, err := redisCli.RedisClient.HVals(context.Background(), key).Result()
	if err != nil {
		return nil, err
	}
	var recommendUserCaches []RecommendUserCache
	for i := 0; i < len(strs); i++ {
		var recommendUserCache RecommendUserCache
		if err := json.Unmarshal([]byte(strs[i]), &recommendUserCache); err != nil {
			mylogrus.MyLog.Errorf("match cache recommendUserCache 转换成结构失败,string:%v, 错误:%v", strs[i], err)
		}
		recommendUserCaches = append(recommendUserCaches, recommendUserCache)
	}
	return recommendUserCaches, nil
}

func GetMatchTradeUnionCache(version string) ([]MatchTradeUnionCache, error) {
	key := GetPreMatchTradeUnion(version)
	strs, err := redisCli.RedisClient.HVals(context.Background(), key).Result()
	if err != nil {
		return nil, err
	}
	var matchTradeUnionCaches []MatchTradeUnionCache
	for i := 0; i < len(strs); i++ {
		var matchTradeUnionCache MatchTradeUnionCache
		if err := json.Unmarshal([]byte(strs[i]), &matchTradeUnionCache); err != nil {
			mylogrus.MyLog.Errorf("match cache matchTradeUnionCache 转换成结构失败,string:%v, 错误:%v", strs[i], err)
		}
		matchTradeUnionCaches = append(matchTradeUnionCaches, matchTradeUnionCache)
	}
	return matchTradeUnionCaches, nil
}

func (matchCycle *MatchCycle) CheckBlock(userId string, otherUserId string) (bool, error) {
	key := GetPrefixMatchBlock(matchCycle.cycle)
	u, _ := strconv.ParseUint(userId, 10, 64)
	o, _ := strconv.ParseUint(otherUserId, 10, 64)
	if u > o {
		flag, err := redisCli.RedisClient.SIsMember(context.Background(), key, userId+"_"+otherUserId).Result()
		if err != nil {
			return flag, err
		}
		return flag, err
	} else {
		flag, err := redisCli.RedisClient.SIsMember(context.Background(), key, otherUserId+"_"+userId).Result()
		if err != nil {
			return flag, err
		}
		return flag, err
	}
}

func (matchCycle *MatchCycle) GetConditionSex(userId string) (string, error) {
	key := GetPrefixMatchConditionSex(matchCycle.cycle)
	str, err := redisCli.RedisClient.HGet(context.Background(), key, userId).Result()
	if err != nil {
		return "", nil
	}
	return str, nil
}

func (matchCycle *MatchCycle) GetConditionCountry(userId string) (string, error) {
	key := GetPrefixMatchConditionCountry(matchCycle.cycle)
	str, err := redisCli.RedisClient.HGet(context.Background(), key, userId).Result()
	if err != nil {
		return "", nil
	}
	return str, nil
}