redis.go 692 B

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