redis.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package bootstrap
  2. import (
  3. "context"
  4. "designs/config"
  5. "designs/global"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "github.com/goccy/go-json"
  9. "go.uber.org/zap"
  10. "os"
  11. "strconv"
  12. "strings"
  13. )
  14. func InitializeRedis() *redis.Client {
  15. client := redis.NewClient(&redis.Options{
  16. Addr: config.Get("redis.host") + ":" + config.Get("redis.port"),
  17. Password: config.Get("redis.password"), // no password set
  18. DB: config.GetInt("redis.db"), // use default DB
  19. //Addr: "localhost:6379", // Redis 服务器地址和端口
  20. //Password: "", // Redis 访问密码,如果没有可以为空字符串
  21. //DB: 0, // 使用的 Redis 数据库编号,默认为 0
  22. })
  23. _, err := client.Ping(context.Background()).Result()
  24. if err != nil {
  25. global.App.Log.Error("Redis connect ping failed, err:", zap.Any("err", err))
  26. return nil
  27. }
  28. return client
  29. }
  30. func UnInitializeRedis(client *redis.Client) {
  31. if client != nil {
  32. client.Close()
  33. }
  34. }
  35. /* 初始游戏配置 */
  36. func InitGameCfg() {
  37. //判读本地有没有数据
  38. res, err := global.Redis().Exists(context.Background(), config.Get("app.province_table")).Result()
  39. if err != nil {
  40. global.App.Log.Error("InitGameCfg err")
  41. }
  42. if res != 1 {
  43. //不存在数据
  44. // 指定目录
  45. filePath := "./config/json/city.json"
  46. bytes, err := os.ReadFile(filePath)
  47. if err != nil {
  48. fmt.Println("os.ReadFile err=", err)
  49. }
  50. data := []interface{}{}
  51. json.Unmarshal(bytes, &data)
  52. // dat := data[0].(map[string]interface{})
  53. // fmt.Println(dat["hid"])
  54. cityData := make(map[string]interface{})
  55. for _, val := range data {
  56. res := val.(map[string]interface{})
  57. // fmt.Println(res["hid"], res["hname"])
  58. cityData[res["hname"].(string)] = res["hid"]
  59. }
  60. //设置数据
  61. global.App.Redis.HMSet(context.Background(), config.Get("app.province_table"), cityData)
  62. }
  63. }
  64. /* 通过ip地址获取地址code */
  65. func FindAddresscodeByIp(ip string) int {
  66. pro, _ := FindAddressByIp(ip)
  67. code := 999
  68. res, err := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
  69. if err != nil {
  70. return code
  71. }
  72. for key, val := range res {
  73. code, _ := strconv.Atoi(val)
  74. if strings.Contains(pro, key) {
  75. return code
  76. }
  77. }
  78. return code
  79. }
  80. /* 通过hid获取地址code */
  81. func FindAddresscodeByHid(hid int) (int, error) {
  82. res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
  83. for _, val := range res {
  84. code, _ := strconv.Atoi(val)
  85. if code == hid {
  86. return hid, nil
  87. }
  88. }
  89. return 0, redis.Nil
  90. }
  91. /* 通过hid获取地址 */
  92. func FindAddressByHid(hid int) string {
  93. res, _ := global.App.Redis.HGetAll(context.Background(), config.Get("app.province_table")).Result()
  94. for key, val := range res {
  95. code, _ := strconv.Atoi(val)
  96. if code == hid {
  97. return key
  98. }
  99. }
  100. return ""
  101. }