Преглед на файлове

[add] 调整图片验证码的存储前缀

bvbej преди 2 години
родител
ревизия
cbaf03cfb2
променени са 1 файла, в които са добавени 11 реда и са изтрити 8 реда
  1. 11 8
      pkg/captcha/base64.go

+ 11 - 8
pkg/captcha/base64.go

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