redis.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * @Descripttion:
  3. * @version:
  4. * @Author: Neo,Huang
  5. * @Date: 2020-10-16 14:29:56
  6. * @LastEditors: Neo,Huang
  7. * @LastEditTime: 2021-12-15 01:45:00
  8. */
  9. package utils
  10. import (
  11. "encoding/json"
  12. "fmt"
  13. "log"
  14. "time"
  15. "github.com/garyburd/redigo/redis"
  16. redigo "github.com/gomodule/redigo/redis"
  17. )
  18. type Redis struct {
  19. pool *redigo.Pool
  20. }
  21. // 连接池
  22. var mapRedisPool = make(map[string](map[int]*Redis))
  23. var mapAccountRedisPool = make(map[string]*Redis)
  24. // 实例数量
  25. var mapRedisCount = make(map[string]int)
  26. // InitRedis 初始化redis
  27. func InitRedis() bool {
  28. count := StringToInt(GetKeyConf("redis", "count"))
  29. mapRedisCount[project] = count
  30. return true
  31. }
  32. // 获取玩家连接池
  33. func GetPlayerRedisPool(uid int) *redigo.Pool {
  34. project := GetProject()
  35. index := uid % mapRedisCount[project]
  36. if mapRedisPool[project] == nil {
  37. mapRedisPool[project] = make(map[int]*Redis)
  38. }
  39. redisConn := mapRedisPool[project][index]
  40. if redisConn == nil {
  41. configFile := GetConfigFile()
  42. redisAddr := GetConf(fmt.Sprintf("redis::host_%d", index), configFile) + ":" + GetConf(fmt.Sprintf("redis::port_%d", index), configFile)
  43. redisAuth := GetConf(fmt.Sprintf("redis::auth_%d", index), configFile)
  44. log.Println(fmt.Sprintf("GetPlayerRedisPool project[%s] connString[%s]", project, redisAddr))
  45. redisConn = new(Redis)
  46. redisConn.pool = &redigo.Pool{
  47. MaxIdle: 10,
  48. IdleTimeout: 300 * time.Second,
  49. MaxActive: 100,
  50. Dial: func() (redigo.Conn, error) {
  51. c, err := redigo.Dial("tcp", redisAddr)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if redisAuth != "" {
  56. _, err := c.Do("AUTH", redisAuth)
  57. if err != nil {
  58. c.Close()
  59. return nil, err
  60. }
  61. }
  62. return c, err
  63. },
  64. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  65. _, err := c.Do("PING")
  66. return err
  67. },
  68. }
  69. mapRedisPool[project][index] = redisConn
  70. }
  71. return redisConn.pool
  72. }
  73. // 获取account redis连接池
  74. func GetAccountRedisPool() *redigo.Pool {
  75. project := GetProject()
  76. redisConn := mapAccountRedisPool[project]
  77. if redisConn == nil {
  78. configFile := GetConfigFile()
  79. redisAddr := GetConf("redis::host_account", configFile) + ":" + GetConf("redis::port_account", configFile)
  80. redisAuth := GetConf("redis::auth_account", configFile)
  81. log.Println(fmt.Sprintf("GetAccountRedisPool project[%s] connString[%s]", project, redisAddr))
  82. redisConn = new(Redis)
  83. redisConn.pool = &redigo.Pool{
  84. MaxIdle: 10,
  85. IdleTimeout: 300 * time.Second,
  86. MaxActive: 100,
  87. Dial: func() (redigo.Conn, error) {
  88. c, err := redigo.Dial("tcp", redisAddr)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if redisAuth != "" {
  93. _, err := c.Do("AUTH", redisAuth)
  94. if err != nil {
  95. c.Close()
  96. return nil, err
  97. }
  98. }
  99. return c, err
  100. },
  101. TestOnBorrow: func(c redigo.Conn, t time.Time) error {
  102. _, err := c.Do("PING")
  103. return err
  104. },
  105. }
  106. mapAccountRedisPool[project] = redisConn
  107. }
  108. return redisConn.pool
  109. }
  110. // Zadd
  111. func Zadd(key string, score int, value string) {
  112. pool := GetPlayerRedisPool(0)
  113. if pool == nil {
  114. fmt.Println("get redis pool fail")
  115. return
  116. }
  117. redisConn := pool.Get()
  118. if redisConn == nil {
  119. fmt.Println("get redis conn fail")
  120. return
  121. }
  122. defer redisConn.Close()
  123. _, err := redisConn.Do("zadd", key, score, value)
  124. if err != nil {
  125. fmt.Println("Zadd err:", err)
  126. }
  127. }
  128. // Hincrby
  129. func Hincrby(key, field string, value int) (err error) {
  130. pool := GetPlayerRedisPool(0)
  131. if pool == nil {
  132. fmt.Println("get redis pool fail")
  133. return ErrGetRedisFail
  134. }
  135. redisConn := pool.Get()
  136. if redisConn == nil {
  137. fmt.Println("get redis conn fail")
  138. return ErrGetRedisConnectFail
  139. }
  140. defer redisConn.Close()
  141. _, err = redisConn.Do("hincrby", key, field, value)
  142. if err != nil {
  143. fmt.Printf("redis hincrby err: %v", err)
  144. return err
  145. }
  146. return
  147. }
  148. // Sadd
  149. func Sadd(key, value string) (err error) {
  150. pool := GetPlayerRedisPool(0)
  151. if pool == nil {
  152. fmt.Println("get redis pool fail")
  153. return ErrGetRedisFail
  154. }
  155. redisConn := pool.Get()
  156. if redisConn == nil {
  157. fmt.Println("get redis conn fail")
  158. return ErrGetRedisConnectFail
  159. }
  160. defer redisConn.Close()
  161. _, err = redisConn.Do("sadd", key, value)
  162. if err != nil {
  163. fmt.Printf("redis sadd err: %v", err)
  164. return err
  165. }
  166. return
  167. }
  168. // Scard
  169. func Scard(key string) (reply interface{}, err error) {
  170. pool := GetPlayerRedisPool(0)
  171. if pool == nil {
  172. fmt.Println("get redis pool fail")
  173. return reply, ErrGetRedisFail
  174. }
  175. redisConn := pool.Get()
  176. if redisConn == nil {
  177. fmt.Println("get redis conn fail")
  178. return reply, ErrGetRedisConnectFail
  179. }
  180. defer redisConn.Close()
  181. reply, err = redisConn.Do("scard", key)
  182. if err != nil {
  183. fmt.Printf("redis scard err: %v", err)
  184. return reply, err
  185. }
  186. return
  187. }
  188. // Hgetall
  189. func Hgetall(key string) (info map[string]string, err error) {
  190. pool := GetPlayerRedisPool(0)
  191. if pool == nil {
  192. fmt.Println("get redis pool fail")
  193. return info, ErrGetRedisFail
  194. }
  195. redisConn := pool.Get()
  196. if redisConn == nil {
  197. fmt.Println("get redis conn fail")
  198. return info, ErrGetRedisConnectFail
  199. }
  200. defer redisConn.Close()
  201. reply, err := redisConn.Do("hgetall", key)
  202. if err != nil {
  203. fmt.Printf("redis hgetall err: %v", err)
  204. return info, err
  205. }
  206. vals, err := redis.Values(reply, err)
  207. if err != nil {
  208. fmt.Printf("redis.Values err: %v", err)
  209. return info, err
  210. }
  211. info = make(map[string]string)
  212. for i := 0; i < len(vals)/2; i++ {
  213. subKey := string(vals[i*2].([]byte))
  214. val := string(vals[i*2+1].([]byte))
  215. info[subKey] = val
  216. }
  217. return
  218. }
  219. // Hget
  220. func Hget(key, field string) (reply interface{}, err error) {
  221. pool := GetPlayerRedisPool(0)
  222. if pool == nil {
  223. fmt.Println("get redis pool fail")
  224. return reply, ErrGetRedisFail
  225. }
  226. redisConn := pool.Get()
  227. if redisConn == nil {
  228. fmt.Println("get redis conn fail")
  229. return reply, ErrGetRedisConnectFail
  230. }
  231. defer redisConn.Close()
  232. reply, err = redisConn.Do("hget", key, field)
  233. if err != nil {
  234. fmt.Printf("redis hget err: %v", err)
  235. return reply, err
  236. }
  237. return
  238. }
  239. // Hset
  240. func Hset(key, field string, value int) (err error) {
  241. pool := GetPlayerRedisPool(0)
  242. if pool == nil {
  243. fmt.Println("get redis pool fail")
  244. return ErrGetRedisFail
  245. }
  246. redisConn := pool.Get()
  247. if redisConn == nil {
  248. fmt.Println("get redis conn fail")
  249. return ErrGetRedisConnectFail
  250. }
  251. defer redisConn.Close()
  252. _, err = redisConn.Do("hset", key, field, value)
  253. if err != nil {
  254. fmt.Printf("redis hset err: %v", err)
  255. return err
  256. }
  257. return
  258. }
  259. // Set
  260. func Set(key, value string) (err error) {
  261. pool := GetPlayerRedisPool(0)
  262. if pool == nil {
  263. fmt.Println("get redis pool fail")
  264. return ErrGetRedisFail
  265. }
  266. redisConn := pool.Get()
  267. if redisConn == nil {
  268. fmt.Println("get redis conn fail")
  269. return ErrGetRedisConnectFail
  270. }
  271. defer redisConn.Close()
  272. _, err = redisConn.Do("set", key, value)
  273. if err != nil {
  274. fmt.Printf("redis hset err: %v", err)
  275. return err
  276. }
  277. return
  278. }
  279. // Get
  280. func Get(key string) (reply interface{}, err error) {
  281. pool := GetPlayerRedisPool(0)
  282. if pool == nil {
  283. fmt.Println("get redis pool fail")
  284. return reply, ErrGetRedisFail
  285. }
  286. redisConn := pool.Get()
  287. if redisConn == nil {
  288. fmt.Println("get redis conn fail")
  289. return reply, ErrGetRedisConnectFail
  290. }
  291. defer redisConn.Close()
  292. reply, err = redisConn.Do("get", key)
  293. if err != nil {
  294. fmt.Printf("redis get err: %v", err)
  295. return reply, err
  296. }
  297. return
  298. }
  299. // 获取系统信息
  300. func GetVersionInfo(version int) map[string]interface{} {
  301. var info = make(map[string]interface{})
  302. pool := GetAccountRedisPool()
  303. if pool == nil {
  304. fmt.Println("get redis pool fail")
  305. return info
  306. }
  307. rd := pool.Get()
  308. defer rd.Close()
  309. subKey := fmt.Sprintf("version:info:%d", version)
  310. val, _ := redis.String(rd.Do("hget", "tb_global:1", subKey))
  311. log.Println("GetVersionInfo val:", val)
  312. if val != "" {
  313. err := json.Unmarshal([]byte(val), &info)
  314. if err != nil {
  315. fmt.Println("GetVersionInfo err:", err)
  316. }
  317. }
  318. return info
  319. }
  320. // 更新系统信息
  321. func UpdateVersionInfo(version int, data map[string]interface{}) bool {
  322. pool := GetAccountRedisPool()
  323. if pool == nil {
  324. fmt.Println("get redis pool fail")
  325. return false
  326. }
  327. rd := pool.Get()
  328. defer rd.Close()
  329. buff, _ := json.Marshal(data)
  330. subKey := fmt.Sprintf("version:info:%d", version)
  331. _, err := rd.Do("hset", "tb_global:1", subKey, buff)
  332. if err != nil {
  333. fmt.Println("UpdateVersionInfo err:", err)
  334. return false
  335. }
  336. return true
  337. }
  338. // 获取玩家昵称
  339. func GetPlayerNickname(uid int) string {
  340. pool := GetPlayerRedisPool(0)
  341. if pool == nil {
  342. fmt.Println("get redis pool fail")
  343. return ""
  344. }
  345. redisConn := pool.Get()
  346. if redisConn == nil {
  347. fmt.Println("get redis conn fail")
  348. return ""
  349. }
  350. defer redisConn.Close()
  351. key := fmt.Sprintf("mdl:user:%d", uid)
  352. val, err := redis.String(redisConn.Do("hget", key, "nickname"))
  353. if err != nil {
  354. fmt.Printf("redis hset err: %v", err)
  355. return ""
  356. }
  357. return val
  358. }