redis.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * @Descripttion:
  3. * @version:
  4. * @Author: Neo,Huang
  5. * @Date: 2020-10-16 14:29:56
  6. * @LastEditors: Neo,Huang
  7. * @LastEditTime: 2020-12-04 11:00:01
  8. */
  9. package utils
  10. import (
  11. "fmt"
  12. "log"
  13. "time"
  14. "github.com/garyburd/redigo/redis"
  15. redigo "github.com/gomodule/redigo/redis"
  16. )
  17. type Redis struct {
  18. pool *redigo.Pool
  19. }
  20. // 连接池
  21. var mapRedisPool = make(map[string](map[int]*Redis))
  22. var mapAccountRedisPool = make(map[string]*Redis)
  23. // 实例数量
  24. var mapRedisCount = make(map[string]int)
  25. // InitRedis 初始化redis
  26. func InitRedis() bool {
  27. count := StringToInt(GetKeyConf("redis", "count"))
  28. mapRedisCount[project] = count
  29. return true
  30. }
  31. // 获取玩家连接池
  32. func GetPlayerRedisPool(uid int) *redigo.Pool {
  33. project := GetProject()
  34. index := uid % mapRedisCount[project]
  35. if mapRedisPool[project] == nil {
  36. mapRedisPool[project] = make(map[int]*Redis)
  37. }
  38. redisConn := mapRedisPool[project][index]
  39. if redisConn == nil {
  40. configFile := GetConfigFile()
  41. redisAddr := GetConf(fmt.Sprintf("redis::host_%d", index), configFile) + ":" + GetConf(fmt.Sprintf("redis::port_%d", index), configFile)
  42. redisAuth := GetConf(fmt.Sprintf("redis::auth_%d", index), configFile)
  43. log.Println(fmt.Sprintf("GetPlayerRedisPool project[%s] connString[%s]", project, redisAddr))
  44. redisConn = new(Redis)
  45. redisConn.pool = &redigo.Pool{
  46. MaxIdle: 10,
  47. IdleTimeout: 300 * time.Second,
  48. MaxActive: 100,
  49. Dial: func() (redigo.Conn, error) {
  50. c, err := redigo.Dial("tcp", redisAddr)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if redisAuth != "" {
  55. _, err := c.Do("AUTH", redisAuth)
  56. if err != nil {
  57. c.Close()
  58. return nil, err
  59. }
  60. }
  61. return c, err
  62. },
  63. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  64. _, err := c.Do("PING")
  65. return err
  66. },
  67. }
  68. mapRedisPool[project][index] = redisConn
  69. }
  70. return redisConn.pool
  71. }
  72. // 获取account redis连接池
  73. func GetAccountRedisPool() *redigo.Pool {
  74. project := GetProject()
  75. redisConn := mapAccountRedisPool[project]
  76. if redisConn == nil {
  77. configFile := GetConfigFile()
  78. redisAddr := GetConf(fmt.Sprintf("redis::host_account"), configFile) + ":" + GetConf(fmt.Sprintf("redis::port_account"), configFile)
  79. redisAuth := GetConf(fmt.Sprintf("redis::auth_account"), configFile)
  80. log.Println(fmt.Sprintf("GetAccountRedisPool project[%s] connString[%s]", project, redisAddr))
  81. redisConn = new(Redis)
  82. redisConn.pool = &redigo.Pool{
  83. MaxIdle: 10,
  84. IdleTimeout: 300 * time.Second,
  85. MaxActive: 100,
  86. Dial: func() (redigo.Conn, error) {
  87. c, err := redigo.Dial("tcp", redisAddr)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if redisAuth != "" {
  92. _, err := c.Do("AUTH", redisAuth)
  93. if err != nil {
  94. c.Close()
  95. return nil, err
  96. }
  97. }
  98. return c, err
  99. },
  100. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  101. _, err := c.Do("PING")
  102. return err
  103. },
  104. }
  105. mapAccountRedisPool[project] = redisConn
  106. }
  107. return redisConn.pool
  108. }
  109. // GetPlayerInfoFromRedis do hget
  110. func GetPlayerInfoFromRedis(uid int, subKey string) string {
  111. pool := GetPlayerRedisPool(uid)
  112. if pool == nil {
  113. fmt.Println("get redis pool fail")
  114. return ""
  115. }
  116. redisConn := pool.Get()
  117. if redisConn == nil {
  118. fmt.Println("get redis conn fail")
  119. return ""
  120. }
  121. defer redisConn.Close()
  122. key := fmt.Sprintf("player:uid:%d", uid)
  123. reply, err := redisConn.Do("hget", key, subKey)
  124. if err != nil {
  125. fmt.Println("GetPlayerInfoFromRedis err:", err)
  126. return ""
  127. }
  128. info, _ := redis.String(reply, err)
  129. // fmt.Println(info)
  130. return info
  131. }
  132. // SetPlayerInfoFromRedis do hget
  133. func SetPlayerInfoFromRedis(uid int, subKey string, val string) bool {
  134. pool := GetPlayerRedisPool(uid)
  135. if pool == nil {
  136. fmt.Println("get redis pool fail")
  137. return false
  138. }
  139. redisConn := pool.Get()
  140. if redisConn == nil {
  141. fmt.Println("get redis conn fail")
  142. return false
  143. }
  144. defer redisConn.Close()
  145. key := fmt.Sprintf("player:uid:%d", uid)
  146. _, err := redisConn.Do("hset", key, subKey, []byte(val))
  147. if err != nil {
  148. fmt.Println("SetPlayerInfoFromRedis err:", err)
  149. return false
  150. }
  151. return true
  152. }
  153. // 设置玩家支付信息
  154. func UpdatePlayerPayInfo(uid int, pay int, payCount int, detail []byte, records []byte) bool {
  155. pool := GetPlayerRedisPool(uid)
  156. if pool == nil {
  157. fmt.Println("get redis pool fail")
  158. return false
  159. }
  160. rd := pool.Get()
  161. defer rd.Close()
  162. key := fmt.Sprintf("player:uid:%d", uid)
  163. val := IntToString(pay)
  164. _, err := rd.Do("hset", key, "pay", []byte(val))
  165. if err != nil {
  166. log.Println("UpdatePlayerPayInfo pay hset fail!", err)
  167. return false
  168. }
  169. val = IntToString(payCount)
  170. _, err = rd.Do("hset", key, "pay_count", []byte(val))
  171. if err != nil {
  172. log.Println("UpdatePlayerPayInfo pay_count hset fail!", err)
  173. return false
  174. }
  175. _, err = rd.Do("hset", key, "js:sd", detail)
  176. if err != nil {
  177. log.Println("UpdatePlayerPayInfo js:sd hset fail!", err)
  178. return false
  179. }
  180. _, err = rd.Do("hset", key, "js:shop", records)
  181. if err != nil {
  182. log.Println("UpdatePlayerPayInfo js:shop hset fail!", err)
  183. return false
  184. }
  185. return true
  186. }
  187. // 删除玩家
  188. func DelPlayerInfo(uid int) bool {
  189. pool := GetPlayerRedisPool(uid)
  190. if pool == nil {
  191. fmt.Println("get redis pool fail")
  192. return false
  193. }
  194. rd := pool.Get()
  195. defer rd.Close()
  196. key := fmt.Sprintf("player:uid:%d", uid)
  197. _, err := rd.Do("del", key)
  198. if err != nil {
  199. log.Println("redis_del_order_info fail!", uid, err)
  200. return false
  201. }
  202. return true
  203. }