You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cls/pkg/captchafx/redis_store.go

42 lines
942 B
Go

1 month ago
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
}