Browse Source

fix:redis缓存使用lua

zqbao 6 months ago
parent
commit
9124f62f62
4 changed files with 48 additions and 24 deletions
  1. 15 23
      controllers/user.go
  2. 4 1
      go.mod
  3. 12 0
      services/lua/verify_code.lua
  4. 17 0
      services/verify_code.go

+ 15 - 23
controllers/user.go

@@ -229,7 +229,7 @@ func (this *UserController) GetVerifyCode() {
 			return
 		}
 
-		phoneCountKey := utils.CACHE_ACCESS_PHONE_COUNT_LOGIN_CODE + req.Email
+		phoneCountKey := utils.CACHE_ACCESS_PHONE_COUNT_LOGIN_CODE + req.AreaCode + req.Phone
 		resCount, _ := go_redis.RedisInt(phoneCountKey)
 		if resCount > utils.VerifyCodeSendLimit {
 			br.Msg = fmt.Sprintf("一天最多获取%s次,已超限", strconv.Itoa(utils.VerifyCodeSendLimit))
@@ -263,19 +263,14 @@ func (this *UserController) GetVerifyCode() {
 				return
 			}
 			br.Msg = "发送成功"
-			isExist := go_redis.IsExist(phoneKey)
-			if isExist {
-				go_redis.Incr(phoneKey)
-				go_redis.Incr(phoneCountKey)
-			} else {
-				go_redis.SetNX(phoneKey, 1, time.Minute*15)
-				go_redis.SetNX(phoneCountKey, 1, utils.SetKeyExpireToday())
+			phoneVerifyCahcheSvc := &services.VerifyCacheIncrService{}
+			err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneKey, 15*int(time.Minute.Seconds()))
+			if err != nil {
+				utils.FileLog.Info("验证码手机号临时缓存失败", err.Error())
 			}
-			isExist = go_redis.IsExist(phoneCountKey)
-			if isExist {
-				go_redis.Incr(phoneCountKey)
-			} else {
-				go_redis.SetNX(phoneCountKey, 1, utils.SetKeyExpireToday())
+			err = phoneVerifyCahcheSvc.VerifyCacheIncr(phoneCountKey, int(utils.SetKeyExpireToday().Seconds()))
+			if err != nil {
+				utils.FileLog.Info("验证码手机号当日缓存失败", err.Error())
 			}
 		}
 	case 2:
@@ -330,17 +325,14 @@ func (this *UserController) GetVerifyCode() {
 				return
 			}
 			br.Msg = "发送成功"
-			isExist := go_redis.IsExist(emailKey)
-			if isExist {
-				go_redis.Incr(emailKey)
-			} else {
-				go_redis.SetNX(emailKey, 1, time.Minute*15)
+			emailVerifyCahcheSvc := &services.VerifyCacheIncrService{}
+			err = emailVerifyCahcheSvc.VerifyCacheIncr(emailKey, 15*int(time.Minute.Seconds()))
+			if err != nil {
+				utils.FileLog.Info("验证码邮箱临时缓存失败, err:", err.Error())
 			}
-			isExist = go_redis.IsExist(emailCountKey)
-			if isExist {
-				go_redis.Incr(emailCountKey)
-			} else {
-				go_redis.SetNX(emailCountKey, 1, utils.SetKeyExpireToday())
+			err = emailVerifyCahcheSvc.VerifyCacheIncr(emailCountKey, int(utils.SetKeyExpireToday().Seconds()))
+			if err != nil {
+				utils.FileLog.Info("验证码邮箱当日缓存失败, err:", err.Error())
 			}
 		} else {
 			br.Msg = "发送失败"

+ 4 - 1
go.mod

@@ -9,6 +9,7 @@ require (
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/go-sql-driver/mysql v1.7.0
+	github.com/mojocn/base64Captcha v1.3.6
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/silenceper/wechat/v2 v2.1.6
 	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
@@ -20,6 +21,7 @@ require (
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 	github.com/fatih/structs v1.1.0 // indirect
+	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/kr/text v0.2.0 // indirect
@@ -37,9 +39,10 @@ require (
 	github.com/tidwall/match v1.1.1 // indirect
 	github.com/tidwall/pretty v1.2.0 // indirect
 	golang.org/x/crypto v0.12.0 // indirect
+	golang.org/x/image v0.13.0 // indirect
 	golang.org/x/net v0.14.0 // indirect
 	golang.org/x/sys v0.11.0 // indirect
-	golang.org/x/text v0.12.0 // indirect
+	golang.org/x/text v0.13.0 // indirect
 	google.golang.org/protobuf v1.30.0 // indirect
 	gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect

+ 12 - 0
services/lua/verify_code.lua

@@ -0,0 +1,12 @@
+-- 定义Lua脚本
+local key = KEYS[1]      -- 从KEYS数组获取要检查的Redis key
+local expireTime = tonumber(ARGV[1]) -- 从ARGV数组获取过期时间
+
+-- 检查key是否存在并执行相应操作
+if redis.call('EXISTS', key) == 1 then
+    -- 存在:将key的值加一
+    return redis.call('INCR', key)
+else
+    -- 不存在:设置key的值设置为1
+    return redis.call('SET', key, 1, 'EX', expireTime) -- 'EX' 设置过期时间(秒)
+end

+ 17 - 0
services/verify_code.go

@@ -0,0 +1,17 @@
+package services
+
+import (
+	"context"
+	_ "embed"
+	"eta/eta_mini_api/utils"
+)
+
+//go:embed lua\verify_code.lua
+var luaVerifyCacheIncr string
+
+type VerifyCacheIncrService struct {
+}
+
+func (v *VerifyCacheIncrService) VerifyCacheIncr(key string, expireInSeconds int) error {
+	return utils.Redis.Eval(context.Background(), luaVerifyCacheIncr, []string{key}, expireInSeconds).Err()
+}