From 28f36b50ffce99d5c5c74eeb0527fecaff9650b9 Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 10 Dec 2021 16:08:46 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9=E4=B8=BAredisClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index bae5569..70acf08 100644 --- a/cache.go +++ b/cache.go @@ -12,6 +12,7 @@ type config struct { Port string Password string DB int + Client *redis.Client } 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 { return func(ca *config) { ca.DB = db @@ -65,11 +72,16 @@ func NewCache(opts ...Option) Cache { rClient := new(cache) - rClient.client = redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%s", cha.Host, cha.Port), - Password: cha.Password, - DB: cha.DB, - }) + if cha.Client == nil { + + rClient.client = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%s", cha.Host, cha.Port), + Password: cha.Password, + DB: cha.DB, + }) + } else { + rClient.client = cha.Client + } rClient.ctx = context.Background()