redis.go 1.64 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package redisCli

import (
	"context"
	"github.com/go-redis/redis/v8"
	"hilo-algoCenter/common/config"
	"hilo-algoCenter/common/mylogrus"
	"log"
)

var RedisClient *redis.Client

var RedisClient1 *redis.Client

func init() {
	RedisClient = redis.NewClient(&redis.Options{
		Addr:         config.GetConfigRedis().REDIS_HOST,
		Password:     config.GetConfigRedis().REDIS_PASSWORD, // no password set
		DB:           0,                                      // use default DB
		PoolSize:     2000,
		MinIdleConns: 200,
	})
	mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_HOST)
	mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_PASSWORD)
	pong, err := RedisClient.Ping(context.Background()).Result()
	if err != nil {
		mylogrus.MyLog.Warn(err)
		mylogrus.MyLog.Fatal("redis db0 connect fail")
	} else {
		mylogrus.MyLog.Info("redis db0 connection success - ", pong)
	}

	RedisClient1 = redis.NewClient(&redis.Options{
		Addr:         config.GetConfigRedis().REDIS_HOST,
		Password:     config.GetConfigRedis().REDIS_PASSWORD, // no password set
		DB:           1,                                      // use default DB
		PoolSize:     200,
		MinIdleConns: 20,
	})
	mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_HOST)
	mylogrus.MyLog.Infoln(config.GetConfigRedis().REDIS_PASSWORD)
	pong, err = RedisClient1.Ping(context.Background()).Result()
	if err != nil {
		mylogrus.MyLog.Warn(err)
		mylogrus.MyLog.Fatal("redis db1 connect fail")
	} else {
		log.Println("redis db1 connection success - " + pong)
	}
	// log hook
	//RedisClient.AddHook(redisHook{})
}

func GetRedis() *redis.Client {
	return RedisClient
}

func GetRedis1() *redis.Client {
	return RedisClient1
}