From 2342a47c6288951f441cd218a9d9988423e35fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=90=89=E5=85=86?= <303176530@qq.com> Date: Sat, 7 Aug 2021 10:16:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20v8=E4=B8=8Bredis=20?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E6=A8=A1=E5=BC=8F=20=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E6=9C=AA=E4=BC=A0=E5=85=A5context=E7=9A=84bu?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/system/sys_captcha.go | 3 ++- server/utils/captcha/redis.go | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/server/api/v1/system/sys_captcha.go b/server/api/v1/system/sys_captcha.go index 0aed33d5..1a357f7d 100644 --- a/server/api/v1/system/sys_captcha.go +++ b/server/api/v1/system/sys_captcha.go @@ -10,7 +10,7 @@ import ( ) // 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码 -// var store = captcha.NewDefaultRedisStore() +//var store = captcha.NewDefaultRedisStore() var store = base64Captcha.DefaultMemStore type BaseApi struct { @@ -27,6 +27,7 @@ func (b *BaseApi) Captcha(c *gin.Context) { // 字符,公式,验证码配置 // 生成默认数字的driver driver := base64Captcha.NewDriverDigit(global.GVA_CONFIG.Captcha.ImgHeight, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.KeyLong, 0.7, 80) + //cp := base64Captcha.NewCaptcha(driver, store.UseWithCtx(c)) // v8下使用redis cp := base64Captcha.NewCaptcha(driver, store) if id, b64s, err := cp.Generate(); err != nil { global.GVA_LOG.Error("验证码获取失败!", zap.Any("err", err)) diff --git a/server/utils/captcha/redis.go b/server/utils/captcha/redis.go index 2e0a906f..67b99ae6 100644 --- a/server/utils/captcha/redis.go +++ b/server/utils/captcha/redis.go @@ -1,14 +1,14 @@ package captcha import ( + "context" "gin-vue-admin/global" - "time" - "github.com/mojocn/base64Captcha" "go.uber.org/zap" + "time" ) -func NewDefaultRedisStore() base64Captcha.Store { +func NewDefaultRedisStore() *RedisStore { return &RedisStore{ Expiration: time.Second * 180, PreKey: "CAPTCHA_", @@ -18,23 +18,29 @@ func NewDefaultRedisStore() base64Captcha.Store { type RedisStore struct { Expiration time.Duration PreKey string + Context context.Context +} + +func (rs *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store { + rs.Context = ctx + return rs } func (rs *RedisStore) Set(id string, value string) { - err := global.GVA_REDIS.Set(rs.PreKey+id, value, rs.Expiration).Err() + err := global.GVA_REDIS.Set(rs.Context, rs.PreKey+id, value, rs.Expiration).Err() if err != nil { global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err)) } } func (rs *RedisStore) Get(key string, clear bool) string { - val, err := global.GVA_REDIS.Get(key).Result() + val, err := global.GVA_REDIS.Get(rs.Context, key).Result() if err != nil { global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err)) return "" } if clear { - err := global.GVA_REDIS.Del(key).Err() + err := global.GVA_REDIS.Del(rs.Context, key).Err() if err != nil { global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err)) return ""