package captchafx import ( "context" "github.com/redis/go-redis/v9" "time" "github.com/mojocn/base64Captcha" ) type RedisStore struct { rdb redis.Cmdable timeOut time.Duration } var _ base64Captcha.Store = (*RedisStore)(nil) func NewRedisStore(cmdable redis.Cmdable) base64Captcha.Store { return &RedisStore{cmdable, time.Minute * 10} } func (r *RedisStore) Set(id string, value string) error { statusCmd := r.rdb.Set(context.Background(), "captcha:"+id, value, r.timeOut) return statusCmd.Err() } func (r *RedisStore) Get(id string, clear bool) string { val, _ := r.rdb.Get(context.Background(), "captcha:"+id).Result() if clear { r.rdb.Del(context.Background(), "captcha:"+id) } return val } func (r *RedisStore) Verify(id, answer string, clear bool) bool { val, _ := r.rdb.Get(context.Background(), "captcha:"+id).Result() if clear { r.rdb.Del(context.Background(), "captcha:"+id) } return val == answer }