修复 v8下redis 验证码模式 存在的未传入context的bug

(cherry picked from commit 2342a47c6288951f441cd218a9d9988423e35fbd)
This commit is contained in:
蒋吉兆
2021-08-07 10:16:11 +08:00
committed by qimiao
parent 14f869bf23
commit 7e4bfc4034
2 changed files with 36 additions and 8 deletions

View File

@@ -1,15 +1,14 @@
package captcha
import (
"time"
"context"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/mojocn/base64Captcha"
"go.uber.org/zap"
"time"
)
func NewDefaultRedisStore() base64Captcha.Store {
func NewDefaultRedisStore() *RedisStore {
return &RedisStore{
Expiration: time.Second * 180,
PreKey: "CAPTCHA_",
@@ -19,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 ""