/* * @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 }