| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package captcha
- import (
- "errors"
- "time"
- "github.com/runningwater/gohub/pkg/app"
- "github.com/runningwater/gohub/pkg/config"
- "github.com/runningwater/gohub/pkg/redis"
- )
- // RedisStore is a store for captcha using Redis.
- // It implements the Store interface. base64Captcha.Store interface.
- type RedisStore struct {
- RedisClient *redis.RedisClient
- KeyPrefix string
- }
- // Set 实现 base64Captcha.Store.Set 方法
- func (s *RedisStore) Set(key string, value string) error {
- expiretime := time.Minute * time.Duration(config.GetInt64("captcha.expire_time"))
- // 方便本地开发调试
- if app.IsLocal() {
- expiretime = time.Minute * time.Duration(config.GetInt64("captcha.debug_expire_time"))
- }
- if ok := s.RedisClient.Set(s.KeyPrefix+key, value, expiretime); !ok {
- return errors.New("无法存储图片验证码")
- }
- return nil
- }
- // Get 实现 base64Captcha.Store.Get 方法
- func (s *RedisStore) Get(key string, clear bool) string {
- key = s.KeyPrefix + key
- val := s.RedisClient.Get(key)
- if clear {
- s.RedisClient.Del(key)
- }
- return val
- }
- // Verify 实现 base64Captcha.Store.Verify 方法
- func (s *RedisStore) Verify(key, answer string, clear bool) bool {
- v := s.Get(key, clear)
- return v == answer
- }
|