123456789101112131415161718192021222324252627282930313233343536373839 |
- package model
- import (
- "app.yhyue.com/moapp/jybase/redis"
- "fmt"
- "math/rand"
- "strings"
- "time"
- )
- func GetRand(n int) int {
- randGen := rand.New(rand.NewSource(time.Now().UnixNano()))
- // 获取一个范围在 [0, ) 的随机整数
- return randGen.Intn(n)
- }
- // SaveHistory 保存历史记录
- func SaveHistory(matchKey string, userId string, redisKey string) bool {
- redisKey = fmt.Sprintf(redisKey, userId)
- history := redis.GetStr("other", redisKey)
- arrS := []string{}
- arrS = strings.Split(history, ",")
- //新增历史记录
- if history == "" {
- arrS = make([]string, 0)
- }
- for k, v := range arrS {
- if v == strings.TrimSpace(matchKey) {
- arrS = append(arrS[:k], arrS[k+1:]...)
- break
- }
- }
- arrS = append(arrS, strings.TrimSpace(matchKey))
- if len(arrS) > 10 {
- arrS = arrS[len(arrS)-10:]
- }
- return redis.Put("other", redisKey, strings.Join(arrS, ","), -1)
- }
|