user_requese.go 594 B

12345678910111213141516171819202122
  1. package cache
  2. import (
  3. "time"
  4. )
  5. // SetUseRequestCount 设置用户请求次数
  6. func (s *UserService) SetUseRequestCount(userId string, current int) {
  7. expiration := time.Now().Add(time.Hour * 24).Truncate(time.Hour * 24)
  8. duration := expiration.Sub(time.Now())
  9. // 设置缓存失效时间为第二天零点
  10. s.cache.Set(userId+"_request", current, duration)
  11. }
  12. // GetUseRequestCount 获取当前用户已请求次数
  13. func (s *UserService) GetUseRequestCount(userId string) int {
  14. sessionContext, ok := s.cache.Get(userId + "_request")
  15. if !ok {
  16. return 0
  17. }
  18. return sessionContext.(int)
  19. }