redis.go 668 字节
package redis

import (
	"context"
	"fmt"
	"strconv"

	"github.com/go-redis/redis/v8"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
)

var rdb *redis.Client

func init() {
	host := constant.REDIS_HOST
	port := constant.REDIS_PORT
	auth := constant.REDIS_AUTH
	index := 0
	if constant.REDIS_DB != "" {
		indexDb := constant.REDIS_DB
		index, _ = strconv.Atoi(indexDb)
	}
	rdb = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%v:%v", host, port),
		Password: auth,
		DB:       index, // use default DB
	})
	res := rdb.Ping(context.TODO())
	if res.Err() != nil {
		panic("redis not found")
	}
}

func GetRDB() *redis.Client {
	return rdb
}