redis.go 814 B

12345678910111213141516171819202122232425262728293031
  1. package init_serve
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/go-redis/redis/v8"
  6. "hongze/hongze_yb_en_api/global"
  7. )
  8. func Redis() {
  9. redisConf := global.CONFIG.Redis
  10. fmt.Println("redisConf.Address", redisConf.Address)
  11. fmt.Println("redisConf.Password", redisConf.Password)
  12. fmt.Println("redisConf.Db", redisConf.Db)
  13. client := redis.NewClient(&redis.Options{
  14. Addr: redisConf.Address,
  15. Password: redisConf.Password,
  16. DB: redisConf.Db,
  17. //PoolSize: 10, //连接池最大socket连接数,默认为10倍CPU数, 10 * runtime.NumCPU(暂不配置)
  18. })
  19. _, err := client.Ping(context.TODO()).Result()
  20. fmt.Println("redis ping:", err)
  21. if err != nil {
  22. global.LOG.Error("redis 链接失败:", err)
  23. panic("redis 链接失败:" + err.Error())
  24. }
  25. //全局赋值redis链接
  26. global.Redis = client
  27. }