redis.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. }
  204. // 获取玩家信息key
  205. func KeyPlayerInfo(uid int) string {
  206. keyPrefix := GetKeyConf("player", "key_player_info_prefix")
  207. if keyPrefix == "" {
  208. keyPrefix = "player:uid:"
  209. }
  210. return fmt.Sprintf("%s%d", keyPrefix, uid)
  211. }
  212. // key数据是否存在
  213. func IsPlayerExistInRedis(uid int) bool {
  214. pool := GetPlayerRedisPool(uid)
  215. if pool == nil {
  216. fmt.Println("get redis pool fail")
  217. return false
  218. }
  219. rd := pool.Get()
  220. defer rd.Close()
  221. key := KeyPlayerInfo(uid)
  222. is_key_exit, _ := redis.Bool(rd.Do("EXISTS", key))
  223. return is_key_exit
  224. }
  225. // Zadd
  226. func Zadd(key string, score int, value string) {
  227. pool := GetPlayerRedisPool(0)
  228. if pool == nil {
  229. fmt.Println("get redis pool fail")
  230. return
  231. }
  232. redisConn := pool.Get()
  233. if redisConn == nil {
  234. fmt.Println("get redis conn fail")
  235. return
  236. }
  237. defer redisConn.Close()
  238. _, err := redisConn.Do("zadd", key, score, value)
  239. if err != nil {
  240. fmt.Println("Zadd err:", err)
  241. }
  242. }
  243. // Zcard
  244. func Zcard(key string) int {
  245. pool := GetPlayerRedisPool(0)
  246. if pool == nil {
  247. fmt.Println("get redis pool fail")
  248. return 0
  249. }
  250. redisConn := pool.Get()
  251. if redisConn == nil {
  252. fmt.Println("get redis conn fail")
  253. return 0
  254. }
  255. defer redisConn.Close()
  256. reply, err := redisConn.Do("zcard", key)
  257. if err != nil {
  258. fmt.Println("Zcard err:", err)
  259. return 0
  260. }
  261. count, _ := redis.Int(reply, err)
  262. return count
  263. }
  264. // Zrange
  265. func Zrange(key string, start int, end int) []string {
  266. pool := GetPlayerRedisPool(0)
  267. if pool == nil {
  268. fmt.Println("get redis pool fail")
  269. return nil
  270. }
  271. redisConn := pool.Get()
  272. if redisConn == nil {
  273. fmt.Println("get redis conn fail")
  274. return nil
  275. }
  276. defer redisConn.Close()
  277. reply, err := redisConn.Do("zrange", key, start, end)
  278. if err != nil {
  279. fmt.Println("Zrange err:", err)
  280. return nil
  281. }
  282. result, _ := redis.Strings(reply, err)
  283. return result
  284. }
  285. // Zremrangebyrank
  286. func Zremrangebyrank(key string, start int, end int) {
  287. pool := GetPlayerRedisPool(0)
  288. if pool == nil {
  289. fmt.Println("get redis pool fail")
  290. return
  291. }
  292. redisConn := pool.Get()
  293. if redisConn == nil {
  294. fmt.Println("get redis conn fail")
  295. return
  296. }
  297. defer redisConn.Close()
  298. _, err := redisConn.Do("zremrangebyrank", key, start, end)
  299. if err != nil {
  300. fmt.Println("Zremrangebyrank err:", err)
  301. }
  302. }
  303. // sadd
  304. func Sadd(key string, val string) {
  305. pool := GetAccountRedisPool()
  306. if pool == nil {
  307. fmt.Println("get redis pool fail")
  308. return
  309. }
  310. rd := pool.Get()
  311. defer rd.Close()
  312. rd.Do("sadd", key, val)
  313. }
  314. // 获取redis key信息
  315. func GetAccountKeyInfo(key string) string {
  316. pool := GetAccountRedisPool()
  317. if pool == nil {
  318. fmt.Println("get redis pool fail")
  319. return ""
  320. }
  321. rd := pool.Get()
  322. defer rd.Close()
  323. val, err1 := redis.String(rd.Do("GET", key))
  324. if err1 != nil {
  325. log.Printf("redis GET %s fail! err:%v", key, err1)
  326. return ""
  327. }
  328. return val
  329. }
  330. // 设置redis key信息
  331. func SetAccountKeyInfo(key string, val string) bool {
  332. pool := GetAccountRedisPool()
  333. if pool == nil {
  334. fmt.Println("get redis pool fail")
  335. return false
  336. }
  337. rd := pool.Get()
  338. defer rd.Close()
  339. _, err := rd.Do("SET", key, val)
  340. if err != nil {
  341. log.Printf("设置 key[%s] 信息fail! err:%s", key, err)
  342. return false
  343. }
  344. return true
  345. }