wangshan 5 months ago
parent
commit
814cc3f701
3 changed files with 83 additions and 30 deletions
  1. 3 28
      captcha/captcha.go
  2. 2 2
      captcha/entity.go
  3. 78 0
      captcha/phoneCache/cache.go

+ 3 - 28
captcha/captcha.go

@@ -10,6 +10,7 @@ package captcha
 **/
 
 import (
+	"app.yhyue.com/moapp/jybase/captcha/phoneCache"
 	"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
 	"fmt"
 	"net/http"
@@ -42,6 +43,7 @@ func InitCaptcha() {
 	ClickBasicCaptcha()
 	ClickShapeCaptcha()
 	RotateCaptcha()
+	go phoneCache.RunTimedTask()
 }
 
 func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
@@ -49,34 +51,7 @@ func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
 		err = fmt.Errorf("phone param is empty or incorrect")
 		return
 	}
-	var times int
-	//CaptLock.Lock()
-	//captLock := CaptLockMap[c.Phone]
-	//if captLock == nil {
-	//	CaptLockMap[c.Phone] = &sync.Mutex{}
-	//	captLock = CaptLockMap[c.Phone]
-	//}
-	//CaptLock.Unlock()
-	//captLock.Lock()
-	//timesPhoneKey := fmt.Sprintf(TimesCacheKey, c.Phone)
-	//timesSessIdKey := fmt.Sprintf(TimesCacheKey, c.Sess.Id())
-	//timesByPhone := redis.GetInt(RedisCode, timesPhoneKey)
-	//timesBySessId := redis.GetInt(RedisCode, timesSessIdKey)
-	//times = func() int {
-	//	if timesBySessId < timesByPhone {
-	//		return timesByPhone
-	//	}
-	//	return timesBySessId
-	//}()
-	//go func(times int, timesPhoneKey, timesSessIdKey string) {
-	//	if times == 0 {
-	//		redis.Put(RedisCode, timesPhoneKey, times, 24*60*60)
-	//		redis.Put(RedisCode, timesSessIdKey, times, 24*60*60)
-	//	}
-	//	redis.Incr(RedisCode, timesPhoneKey)
-	//	redis.Incr(RedisCode, timesSessIdKey)
-	//}(times, timesPhoneKey, timesSessIdKey)
-	//captLock.Unlock()
+	var times = phoneCache.GetAccessCount(c.Phone)
 	switch {
 	case times < MiniTimes:
 		return c.GetSlideCaptchaData()

+ 2 - 2
captcha/entity.go

@@ -8,8 +8,8 @@ import (
 )
 
 const (
-	MiniTimes       = 3
-	MaxTimes        = 7
+	MiniTimes       = 4
+	MaxTimes        = 8
 	Padding   int64 = 4
 )
 

+ 78 - 0
captcha/phoneCache/cache.go

@@ -0,0 +1,78 @@
+package phoneCache
+
+import (
+	"log"
+	"sync"
+	"time"
+)
+
+type UserCache struct {
+	Lock        sync.Mutex
+	AccessCount int
+	LastAccess  time.Time
+}
+
+var lockCache sync.Map
+
+// 根据手机号获取或者创建锁
+func GetOrCreateUserCache(phone string) (userCache *UserCache) {
+	if value, exists := lockCache.Load(phone); exists {
+		userCache = value.(*UserCache)
+		userCache.Lock.Lock()
+		userCache.LastAccess = time.Now()
+		userCache.AccessCount++
+		userCache.Lock.Unlock()
+		return userCache
+	}
+	userCache = &UserCache{
+		Lock:        sync.Mutex{},
+		AccessCount: 0,
+		LastAccess:  time.Now(),
+	}
+	_, loaded := lockCache.LoadOrStore(phone, userCache)
+	if !loaded {
+		log.Println("load or store false,", phone)
+	}
+	return
+}
+
+// 获取手机号访问次数
+func GetAccessCount(phone string) int {
+	userCache := GetOrCreateUserCache(phone)
+	userCache.Lock.Lock()
+	defer userCache.Lock.Unlock()
+	return userCache.AccessCount
+}
+
+//// 增加手机号访问次数
+//func IncrementAccessCount(phone string) {
+//	userCache := GetOrCreateUserCache(phone)
+//	userCache.Lock.Lock()
+//	defer userCache.Lock.Unlock()
+//	userCache.AccessCount++
+//	userCache.LastAccess = time.Now()
+//}
+
+// 清理过期缓存
+func ClearExpireCache() {
+	lockCache.Range(func(key, value any) bool {
+		userCache := value.(*UserCache)
+		userCache.Lock.Lock()
+		lastAccess := userCache.LastAccess
+		userCache.Lock.Unlock()
+		if time.Since(lastAccess) > 24*60*60 {
+			lockCache.Delete(key)
+		}
+		return true
+	})
+}
+
+// RunTimedTask .
+func RunTimedTask() {
+	ticker := time.NewTicker(time.Minute * 30)
+	go func() {
+		for range ticker.C {
+			ClearExpireCache()
+		}
+	}()
+}