mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 00:05:15 +00:00
allow redis persistence config could be created with an existing redis client
This commit is contained in:
parent
a548596c16
commit
83ab00a601
|
@ -1,6 +1,10 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
type PersistenceService interface {
|
type PersistenceService interface {
|
||||||
NewStore(id string, subIDs ...string) Store
|
NewStore(id string, subIDs ...string) Store
|
||||||
|
@ -22,6 +26,10 @@ type RedisPersistenceConfig struct {
|
||||||
Password string `yaml:"password,omitempty" json:"password,omitempty" env:"REDIS_PASSWORD"`
|
Password string `yaml:"password,omitempty" json:"password,omitempty" env:"REDIS_PASSWORD"`
|
||||||
DB int `yaml:"db" json:"db" env:"REDIS_DB"`
|
DB int `yaml:"db" json:"db" env:"REDIS_DB"`
|
||||||
Namespace string `yaml:"namespace" json:"namespace" env:"REDIS_NAMESPACE"`
|
Namespace string `yaml:"namespace" json:"namespace" env:"REDIS_NAMESPACE"`
|
||||||
|
|
||||||
|
// Redis is the redis client field
|
||||||
|
// this field is optional, only used when you want to set the redis client instance in the runtime
|
||||||
|
Redis *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonPersistenceConfig struct {
|
type JsonPersistenceConfig struct {
|
||||||
|
|
|
@ -22,13 +22,18 @@ type RedisPersistenceService struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRedisPersistenceService(config *RedisPersistenceConfig) *RedisPersistenceService {
|
func NewRedisPersistenceService(config *RedisPersistenceConfig) *RedisPersistenceService {
|
||||||
client := redis.NewClient(&redis.Options{
|
var client *redis.Client
|
||||||
Addr: net.JoinHostPort(config.Host, config.Port),
|
if config.Redis != nil {
|
||||||
// Username: "", // username is only for redis 6.0
|
client = config.Redis
|
||||||
// pragma: allowlist nextline secret
|
} else {
|
||||||
Password: config.Password, // no password set
|
client = redis.NewClient(&redis.Options{
|
||||||
DB: config.DB, // use default DB
|
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||||
})
|
// Username: "", // username is only for redis 6.0
|
||||||
|
// pragma: allowlist nextline secret
|
||||||
|
Password: config.Password, // no password set
|
||||||
|
DB: config.DB, // use default DB
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return &RedisPersistenceService{
|
return &RedisPersistenceService{
|
||||||
redis: client,
|
redis: client,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user