Przeglądaj źródła

wip:captcha缓存

wangshan 5 miesięcy temu
rodzic
commit
019656d96e
2 zmienionych plików z 80 dodań i 28 usunięć
  1. 2 28
      captcha/captcha.go
  2. 78 0
      captcha/userCache/cache.go

+ 2 - 28
captcha/captcha.go

@@ -42,6 +42,7 @@ func InitCaptcha() {
 	ClickBasicCaptcha()
 	ClickShapeCaptcha()
 	RotateCaptcha()
+	//go userCache.RunTimedTask()
 }
 
 func (c *GetCaptcha) GetCaptchaData() (cd CaptRes, err error) {
@@ -49,34 +50,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 int //userCache.GetAccessCount(c.Phone)
 	switch {
 	case times < MiniTimes:
 		return c.GetSlideCaptchaData()

+ 78 - 0
captcha/userCache/cache.go

@@ -0,0 +1,78 @@
+package userCache
+
+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()
+		}
+	}()
+}