|
@ -1,14 +1,14 @@ |
|
|
package captcha |
|
|
package captcha |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"context" |
|
|
"gin-vue-admin/global" |
|
|
"gin-vue-admin/global" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/mojocn/base64Captcha" |
|
|
"github.com/mojocn/base64Captcha" |
|
|
"go.uber.org/zap" |
|
|
"go.uber.org/zap" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func NewDefaultRedisStore() base64Captcha.Store { |
|
|
|
|
|
|
|
|
func NewDefaultRedisStore() *RedisStore { |
|
|
return &RedisStore{ |
|
|
return &RedisStore{ |
|
|
Expiration: time.Second * 180, |
|
|
Expiration: time.Second * 180, |
|
|
PreKey: "CAPTCHA_", |
|
|
PreKey: "CAPTCHA_", |
|
@ -18,23 +18,29 @@ func NewDefaultRedisStore() base64Captcha.Store { |
|
|
type RedisStore struct { |
|
|
type RedisStore struct { |
|
|
Expiration time.Duration |
|
|
Expiration time.Duration |
|
|
PreKey string |
|
|
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) { |
|
|
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 { |
|
|
if err != nil { |
|
|
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err)) |
|
|
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err)) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (rs *RedisStore) Get(key string, clear bool) string { |
|
|
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 { |
|
|
if err != nil { |
|
|
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err)) |
|
|
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err)) |
|
|
return "" |
|
|
return "" |
|
|
} |
|
|
} |
|
|
if clear { |
|
|
if clear { |
|
|
err := global.GVA_REDIS.Del(key).Err() |
|
|
|
|
|
|
|
|
err := global.GVA_REDIS.Del(rs.Context, key).Err() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err)) |
|
|
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err)) |
|
|
return "" |
|
|
return "" |
|
|