|
@@ -1,6 +1,7 @@
|
|
package captcha
|
|
package captcha
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
"git.bvbej.com/bvbej/base-golang/pkg/cache"
|
|
"git.bvbej.com/bvbej/base-golang/pkg/cache"
|
|
"github.com/mojocn/base64Captcha"
|
|
"github.com/mojocn/base64Captcha"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
@@ -8,18 +9,18 @@ import (
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
-const RedisKeyPrefixCaptcha = "captcha:base64:"
|
|
|
|
-
|
|
|
|
var _ base64Captcha.Store = (*store)(nil)
|
|
var _ base64Captcha.Store = (*store)(nil)
|
|
|
|
|
|
type store struct {
|
|
type store struct {
|
|
cache cache.Repo
|
|
cache cache.Repo
|
|
ttl time.Duration
|
|
ttl time.Duration
|
|
logger *zap.Logger
|
|
logger *zap.Logger
|
|
|
|
+ ns string
|
|
|
|
+ prefix string
|
|
}
|
|
}
|
|
|
|
|
|
func (s *store) Set(id string, value string) error {
|
|
func (s *store) Set(id string, value string) error {
|
|
- err := s.cache.Set(RedisKeyPrefixCaptcha+id, value, s.ttl)
|
|
|
|
|
|
+ err := s.cache.Set(fmt.Sprintf("%s%s%s", s.ns, s.prefix, id), value, s.ttl)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -27,9 +28,9 @@ func (s *store) Set(id string, value string) error {
|
|
}
|
|
}
|
|
|
|
|
|
func (s *store) Get(id string, clear bool) string {
|
|
func (s *store) Get(id string, clear bool) string {
|
|
- value, err := s.cache.Get(RedisKeyPrefixCaptcha + id)
|
|
|
|
|
|
+ value, err := s.cache.Get(fmt.Sprintf("%s%s%s", s.ns, s.prefix, id))
|
|
if err == nil && clear {
|
|
if err == nil && clear {
|
|
- s.cache.Del(RedisKeyPrefixCaptcha + id)
|
|
|
|
|
|
+ s.cache.Del(fmt.Sprintf("%s%s%s", s.ns, s.prefix, id))
|
|
}
|
|
}
|
|
return value
|
|
return value
|
|
}
|
|
}
|
|
@@ -42,10 +43,12 @@ func (s *store) Verify(id, answer string, clear bool) bool {
|
|
return strings.ToLower(value) == strings.ToLower(answer)
|
|
return strings.ToLower(value) == strings.ToLower(answer)
|
|
}
|
|
}
|
|
|
|
|
|
-func NewStore(cache cache.Repo, ttl time.Duration) base64Captcha.Store {
|
|
|
|
|
|
+func NewStore(cache cache.Repo, ttl time.Duration, namespace string) base64Captcha.Store {
|
|
return &store{
|
|
return &store{
|
|
- cache: cache,
|
|
|
|
- ttl: ttl,
|
|
|
|
|
|
+ cache: cache,
|
|
|
|
+ ttl: ttl,
|
|
|
|
+ ns: namespace,
|
|
|
|
+ prefix: "captcha:base64:",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|