34 lines
624 B
Go
34 lines
624 B
Go
|
package cache
|
||
|
|
||
|
import (
|
||
|
"github.com/go-redis/redis"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// RedisClient Redis缓存客户端单例
|
||
|
var (
|
||
|
RedisClient *redis.Client
|
||
|
)
|
||
|
|
||
|
type RedisConfig struct {
|
||
|
RedisDB string
|
||
|
RedisAddr string
|
||
|
RedisPw string
|
||
|
RedisDbName string
|
||
|
}
|
||
|
|
||
|
//LoadRedis 在中间件中初始化redis链接
|
||
|
func LoadRedis(configEnv RedisConfig) {
|
||
|
db, _ := strconv.ParseUint(configEnv.RedisDbName, 10, 64)
|
||
|
client := redis.NewClient(&redis.Options{
|
||
|
Addr: configEnv.RedisAddr,
|
||
|
Password: configEnv.RedisPw,
|
||
|
DB: int(db),
|
||
|
})
|
||
|
_, err := client.Ping().Result()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
RedisClient = client
|
||
|
}
|