123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- /*
- * @Descripttion:
- * @version:
- * @Author: Neo,Huang
- * @Date: 2020-10-16 14:29:56
- * @LastEditors: Neo,Huang
- * @LastEditTime: 2020-12-04 11:00:01
- */
- package utils
- import (
- "fmt"
- "log"
- "time"
- "github.com/garyburd/redigo/redis"
- redigo "github.com/gomodule/redigo/redis"
- )
- type Redis struct {
- pool *redigo.Pool
- }
- // 连接池
- var mapRedisPool = make(map[string](map[int]*Redis))
- var mapAccountRedisPool = make(map[string]*Redis)
- // 实例数量
- var mapRedisCount = make(map[string]int)
- // InitRedis 初始化redis
- func InitRedis() bool {
- count := StringToInt(GetKeyConf("redis", "count"))
- mapRedisCount[project] = count
- return true
- }
- // 获取玩家连接池
- func GetPlayerRedisPool(uid int) *redigo.Pool {
- project := GetProject()
- index := uid % mapRedisCount[project]
- if mapRedisPool[project] == nil {
- mapRedisPool[project] = make(map[int]*Redis)
- }
- redisConn := mapRedisPool[project][index]
- if redisConn == nil {
- configFile := GetConfigFile()
- redisAddr := GetConf(fmt.Sprintf("redis::host_%d", index), configFile) + ":" + GetConf(fmt.Sprintf("redis::port_%d", index), configFile)
- redisAuth := GetConf(fmt.Sprintf("redis::auth_%d", index), configFile)
- log.Println(fmt.Sprintf("GetPlayerRedisPool project[%s] connString[%s]", project, redisAddr))
- redisConn = new(Redis)
- redisConn.pool = &redigo.Pool{
- MaxIdle: 10,
- IdleTimeout: 300 * time.Second,
- MaxActive: 100,
- Dial: func() (redigo.Conn, error) {
- c, err := redigo.Dial("tcp", redisAddr)
- if err != nil {
- return nil, err
- }
- if redisAuth != "" {
- _, err := c.Do("AUTH", redisAuth)
- if err != nil {
- c.Close()
- return nil, err
- }
- }
- return c, err
- },
- TestOnBorrow: func(c redigo.Conn, t time.Time) error {
- _, err := c.Do("PING")
- return err
- },
- }
- mapRedisPool[project][index] = redisConn
- }
- return redisConn.pool
- }
- // 获取account redis连接池
- func GetAccountRedisPool() *redigo.Pool {
- project := GetProject()
- redisConn := mapAccountRedisPool[project]
- if redisConn == nil {
- configFile := GetConfigFile()
- redisAddr := GetConf(fmt.Sprintf("redis::host_account"), configFile) + ":" + GetConf(fmt.Sprintf("redis::port_account"), configFile)
- redisAuth := GetConf(fmt.Sprintf("redis::auth_account"), configFile)
- log.Println(fmt.Sprintf("GetAccountRedisPool project[%s] connString[%s]", project, redisAddr))
- redisConn = new(Redis)
- redisConn.pool = &redigo.Pool{
- MaxIdle: 10,
- IdleTimeout: 300 * time.Second,
- MaxActive: 100,
- Dial: func() (redigo.Conn, error) {
- c, err := redigo.Dial("tcp", redisAddr)
- if err != nil {
- return nil, err
- }
- if redisAuth != "" {
- _, err := c.Do("AUTH", redisAuth)
- if err != nil {
- c.Close()
- return nil, err
- }
- }
- return c, err
- },
- TestOnBorrow: func(c redigo.Conn, t time.Time) error {
- _, err := c.Do("PING")
- return err
- },
- }
- mapAccountRedisPool[project] = redisConn
- }
- return redisConn.pool
- }
- // GetPlayerInfoFromRedis do hget
- func GetPlayerInfoFromRedis(uid int, subKey string) string {
- pool := GetPlayerRedisPool(uid)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return ""
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return ""
- }
- defer redisConn.Close()
- key := fmt.Sprintf("player:uid:%d", uid)
- reply, err := redisConn.Do("hget", key, subKey)
- if err != nil {
- fmt.Println("GetPlayerInfoFromRedis err:", err)
- return ""
- }
- info, _ := redis.String(reply, err)
- // fmt.Println(info)
- return info
- }
- // SetPlayerInfoFromRedis do hget
- func SetPlayerInfoFromRedis(uid int, subKey string, val string) bool {
- pool := GetPlayerRedisPool(uid)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return false
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return false
- }
- defer redisConn.Close()
- key := fmt.Sprintf("player:uid:%d", uid)
- _, err := redisConn.Do("hset", key, subKey, []byte(val))
- if err != nil {
- fmt.Println("SetPlayerInfoFromRedis err:", err)
- return false
- }
- return true
- }
- // 设置玩家支付信息
- func UpdatePlayerPayInfo(uid int, pay int, payCount int, detail []byte, records []byte) bool {
- pool := GetPlayerRedisPool(uid)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return false
- }
- rd := pool.Get()
- defer rd.Close()
- key := fmt.Sprintf("player:uid:%d", uid)
- val := IntToString(pay)
- _, err := rd.Do("hset", key, "pay", []byte(val))
- if err != nil {
- log.Println("UpdatePlayerPayInfo pay hset fail!", err)
- return false
- }
- val = IntToString(payCount)
- _, err = rd.Do("hset", key, "pay_count", []byte(val))
- if err != nil {
- log.Println("UpdatePlayerPayInfo pay_count hset fail!", err)
- return false
- }
- _, err = rd.Do("hset", key, "js:sd", detail)
- if err != nil {
- log.Println("UpdatePlayerPayInfo js:sd hset fail!", err)
- return false
- }
- _, err = rd.Do("hset", key, "js:shop", records)
- if err != nil {
- log.Println("UpdatePlayerPayInfo js:shop hset fail!", err)
- return false
- }
- return true
- }
- // 删除玩家
- func DelPlayerInfo(uid int) bool {
- pool := GetPlayerRedisPool(uid)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return false
- }
- rd := pool.Get()
- defer rd.Close()
- key := fmt.Sprintf("player:uid:%d", uid)
- _, err := rd.Do("del", key)
- if err != nil {
- log.Println("redis_del_order_info fail!", uid, err)
- return false
- }
- return true
- }
- // 获取玩家信息key
- func KeyPlayerInfo(uid int) string {
- keyPrefix := GetKeyConf("player", "key_player_info_prefix")
- if keyPrefix == "" {
- keyPrefix = "player:uid:"
- }
- return fmt.Sprintf("%s%d", keyPrefix, uid)
- }
- // key数据是否存在
- func IsPlayerExistInRedis(uid int) bool {
- pool := GetPlayerRedisPool(uid)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return false
- }
- rd := pool.Get()
- defer rd.Close()
- key := KeyPlayerInfo(uid)
- is_key_exit, _ := redis.Bool(rd.Do("EXISTS", key))
- return is_key_exit
- }
- // Zadd
- func Zadd(key string, score int, value string) {
- pool := GetPlayerRedisPool(0)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return
- }
- defer redisConn.Close()
- _, err := redisConn.Do("zadd", key, score, value)
- if err != nil {
- fmt.Println("Zadd err:", err)
- }
- }
- // Zcard
- func Zcard(key string) int {
- pool := GetPlayerRedisPool(0)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return 0
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return 0
- }
- defer redisConn.Close()
- reply, err := redisConn.Do("zcard", key)
- if err != nil {
- fmt.Println("Zcard err:", err)
- return 0
- }
- count, _ := redis.Int(reply, err)
- return count
- }
- // Zrange
- func Zrange(key string, start int, end int) []string {
- pool := GetPlayerRedisPool(0)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return nil
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return nil
- }
- defer redisConn.Close()
- reply, err := redisConn.Do("zrange", key, start, end)
- if err != nil {
- fmt.Println("Zrange err:", err)
- return nil
- }
- result, _ := redis.Strings(reply, err)
- return result
- }
- // Zremrangebyrank
- func Zremrangebyrank(key string, start int, end int) {
- pool := GetPlayerRedisPool(0)
- if pool == nil {
- fmt.Println("get redis pool fail")
- return
- }
- redisConn := pool.Get()
- if redisConn == nil {
- fmt.Println("get redis conn fail")
- return
- }
- defer redisConn.Close()
- _, err := redisConn.Do("zremrangebyrank", key, start, end)
- if err != nil {
- fmt.Println("Zremrangebyrank err:", err)
- }
- }
- // sadd
- func Sadd(key string, val string) {
- pool := GetAccountRedisPool()
- if pool == nil {
- fmt.Println("get redis pool fail")
- return
- }
- rd := pool.Get()
- defer rd.Close()
- rd.Do("sadd", key, val)
- }
- // 获取redis key信息
- func GetAccountKeyInfo(key string) string {
- pool := GetAccountRedisPool()
- if pool == nil {
- fmt.Println("get redis pool fail")
- return ""
- }
- rd := pool.Get()
- defer rd.Close()
- val, err1 := redis.String(rd.Do("GET", key))
- if err1 != nil {
- log.Printf("redis GET %s fail! err:%v", key, err1)
- return ""
- }
- return val
- }
- // 设置redis key信息
- func SetAccountKeyInfo(key string, val string) bool {
- pool := GetAccountRedisPool()
- if pool == nil {
- fmt.Println("get redis pool fail")
- return false
- }
- rd := pool.Get()
- defer rd.Close()
- _, err := rd.Do("SET", key, val)
- if err != nil {
- log.Printf("设置 key[%s] 信息fail! err:%s", key, err)
- return false
- }
- return true
- }
|