37 lines
738 B
Go
37 lines
738 B
Go
package client
|
|
|
|
import (
|
|
"Blog/bootstrap"
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var redisClient *redis.Client
|
|
var redisInitMutex *sync.Mutex = &sync.Mutex{}
|
|
|
|
func RedisClient() *redis.Client {
|
|
if redisClient == nil {
|
|
redisInitMutex.Lock()
|
|
if redisClient == nil {
|
|
addr := bootstrap.Config.Database.Redis.Addr
|
|
db := bootstrap.Config.Database.Redis.Db
|
|
redisClient = redis.NewClient(&redis.Options{
|
|
Addr: addr,
|
|
DB: db, // 默认DB 0
|
|
})
|
|
ctx := context.Background()
|
|
sc := redisClient.Ping(ctx)
|
|
pong := sc.Val()
|
|
if pong == "PONG" {
|
|
logrus.Info("Redis连接成功")
|
|
redisClient.FlushDB(ctx)
|
|
}
|
|
}
|
|
redisInitMutex.Unlock()
|
|
}
|
|
return redisClient
|
|
}
|