mirror of
https://github.com/cowardmrx/rds_cache_go.git
synced 2025-01-18 18:55:56 +08:00
⚡新增一个配置选项为redisClient
This commit is contained in:
parent
94a005b4b6
commit
28f36b50ff
22
cache.go
22
cache.go
@ -12,6 +12,7 @@ type config struct {
|
|||||||
Port string
|
Port string
|
||||||
Password string
|
Password string
|
||||||
DB int
|
DB int
|
||||||
|
Client *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(ca *config)
|
type Option func(ca *config)
|
||||||
@ -34,6 +35,12 @@ func WithPassword(password string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithRedisClient(client *redis.Client) Option {
|
||||||
|
return func(ca *config) {
|
||||||
|
ca.Client = client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithDB(db int) Option {
|
func WithDB(db int) Option {
|
||||||
return func(ca *config) {
|
return func(ca *config) {
|
||||||
ca.DB = db
|
ca.DB = db
|
||||||
@ -65,11 +72,16 @@ func NewCache(opts ...Option) Cache {
|
|||||||
|
|
||||||
rClient := new(cache)
|
rClient := new(cache)
|
||||||
|
|
||||||
rClient.client = redis.NewClient(&redis.Options{
|
if cha.Client == nil {
|
||||||
Addr: fmt.Sprintf("%s:%s", cha.Host, cha.Port),
|
|
||||||
Password: cha.Password,
|
rClient.client = redis.NewClient(&redis.Options{
|
||||||
DB: cha.DB,
|
Addr: fmt.Sprintf("%s:%s", cha.Host, cha.Port),
|
||||||
})
|
Password: cha.Password,
|
||||||
|
DB: cha.DB,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
rClient.client = cha.Client
|
||||||
|
}
|
||||||
|
|
||||||
rClient.ctx = context.Background()
|
rClient.ctx = context.Background()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user