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 Save interface. base64Captcha.Store interface. type RedisStore struct { RedisClient *redis.Client KeyPrefix string } // Set 实现 base64Captcha.Save.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.Save.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.Save.Verify 方法 func (s *RedisStore) Verify(key, answer string, clear bool) bool { v := s.Get(key, clear) return v == answer }