mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-17 16:43:54 +00:00
feat: support Redis Sentinel and Redis Cluster (#1952)
* feature: support Redis Sentinel and Redis Cluster * chore: update implementation --------- Co-authored-by: JustSong <quanpengsong@gmail.com>
This commit is contained in:
@@ -2,13 +2,15 @@ package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var RDB *redis.Client
|
||||
var RDB redis.Cmdable
|
||||
var RedisEnabled = true
|
||||
|
||||
// InitRedisClient This function is called after init()
|
||||
@@ -23,13 +25,23 @@ func InitRedisClient() (err error) {
|
||||
logger.SysLog("SYNC_FREQUENCY not set, Redis is disabled")
|
||||
return nil
|
||||
}
|
||||
logger.SysLog("Redis is enabled")
|
||||
opt, err := redis.ParseURL(os.Getenv("REDIS_CONN_STRING"))
|
||||
if err != nil {
|
||||
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
|
||||
redisConnString := os.Getenv("REDIS_CONN_STRING")
|
||||
if os.Getenv("REDIS_MASTER_NAME") == "" {
|
||||
logger.SysLog("Redis is enabled")
|
||||
opt, err := redis.ParseURL(redisConnString)
|
||||
if err != nil {
|
||||
logger.FatalLog("failed to parse Redis connection string: " + err.Error())
|
||||
}
|
||||
RDB = redis.NewClient(opt)
|
||||
} else {
|
||||
// cluster mode
|
||||
logger.SysLog("Redis cluster mode enabled")
|
||||
RDB = redis.NewUniversalClient(&redis.UniversalOptions{
|
||||
Addrs: strings.Split(redisConnString, ","),
|
||||
Password: os.Getenv("REDIS_PASSWORD"),
|
||||
MasterName: os.Getenv("REDIS_MASTER_NAME"),
|
||||
})
|
||||
}
|
||||
RDB = redis.NewClient(opt)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
Reference in New Issue
Block a user