userfreecount.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package combomatch
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/redis"
  5. "encoding/json"
  6. "strconv"
  7. "sync"
  8. "time"
  9. )
  10. var freeCountMgr *userfreecount
  11. func getFreeCountManager() *userfreecount {
  12. if freeCountMgr == nil {
  13. freeCountMgr = new(userfreecount)
  14. }
  15. return freeCountMgr
  16. }
  17. type freeInfo struct {
  18. MatchId int
  19. FreeCount int
  20. }
  21. type userfreecount struct {
  22. lock *sync.RWMutex
  23. userFreeInfo map[int][]freeInfo
  24. isDirty bool
  25. lastCheckDay int
  26. }
  27. const refresh_usercount_config_sec = 600
  28. func (m *userfreecount) run() {
  29. log.Release("combomatch.userfreecount.run()")
  30. m.lock = &sync.RWMutex{}
  31. m.userFreeInfo = make(map[int][]freeInfo)
  32. m.loadFromRedis()
  33. m.lastCheckDay = time.Now().Day()
  34. m.flush()
  35. }
  36. func getUserFreeCountRedisKey() string {
  37. return "combomatch:userfreecount"
  38. }
  39. func (m *userfreecount) loadFromRedis() {
  40. data, ok := redis.String_Get(getUserFreeCountRedisKey())
  41. if data == "" || !ok {
  42. return
  43. }
  44. m.lock.Lock()
  45. err := json.Unmarshal([]byte(data), &m.userFreeInfo)
  46. m.lock.Unlock()
  47. if err != nil {
  48. log.Release("userfreecount.loadFromRedis Unmarshal failed err:%v,%s", err, data)
  49. return
  50. }
  51. }
  52. func (m *userfreecount) doFlush() {
  53. m.isDirty = true
  54. m.flush()
  55. }
  56. func (m *userfreecount) flush() {
  57. time.AfterFunc(refresh_usercount_config_sec*time.Second, m.flush)
  58. m.lock.RLock()
  59. if !m.isDirty {
  60. m.lock.RUnlock()
  61. return
  62. }
  63. m.isDirty = false
  64. d, _ := json.Marshal(m.userFreeInfo)
  65. m.lock.RUnlock()
  66. go redis.String_Set(getUserFreeCountRedisKey(), string(d))
  67. }
  68. func (m *userfreecount) dump(userIdStr string) {
  69. log.Release("-------------------------------")
  70. log.Release("userfreecount.dumpUser[%s]", userIdStr)
  71. defer func() {
  72. log.Release("+++++++++++++++++++++++++++++++")
  73. log.Release("")
  74. }()
  75. userId, err := strconv.Atoi(userIdStr)
  76. if err != nil {
  77. log.Release(" atoi error %v", err)
  78. return
  79. }
  80. fi := m.getUserFreeInfo(userId)
  81. if len(fi) == 0 {
  82. log.Release(" user has no free count")
  83. return
  84. }
  85. for _, v := range fi {
  86. log.Release(" MatchId[%d],Count[%d]", v.MatchId, v.FreeCount)
  87. }
  88. }
  89. func (m *userfreecount) addFreeCount(userId int, matchId int) {
  90. m.checkDayRefresh()
  91. fi := m.getUserFreeInfo(userId)
  92. added := false
  93. for i := 0; i < len(fi); i++ {
  94. if fi[i].MatchId == matchId {
  95. fi[i].FreeCount++
  96. added = true
  97. break
  98. }
  99. }
  100. // 之前没有
  101. if !added {
  102. fi = append(fi, freeInfo{MatchId: matchId, FreeCount: 1})
  103. }
  104. m.lock.Lock()
  105. m.userFreeInfo[userId] = fi
  106. m.lock.Unlock()
  107. m.isDirty = true
  108. return
  109. }
  110. func (m *userfreecount) reduceFreeCount(userId int, matchId int) {
  111. m.checkDayRefresh()
  112. fi := m.getUserFreeInfo(userId)
  113. for i := 0; i < len(fi); i++ {
  114. if fi[i].MatchId == matchId {
  115. fi[i].FreeCount--
  116. if fi[i].FreeCount < 0 {
  117. fi[i].FreeCount = 0
  118. }
  119. break
  120. }
  121. }
  122. m.lock.Lock()
  123. m.userFreeInfo[userId] = fi
  124. m.lock.Unlock()
  125. m.isDirty = true
  126. return
  127. }
  128. func (m *userfreecount) checkDayRefresh() {
  129. nowDay := time.Now().Day()
  130. if nowDay == m.lastCheckDay {
  131. return
  132. }
  133. m.lastCheckDay = nowDay
  134. // 清理数据
  135. m.lock.Lock()
  136. m.userFreeInfo = make(map[int][]freeInfo)
  137. m.lock.Unlock()
  138. m.isDirty = true
  139. }
  140. func (m *userfreecount) getUserFreeInfo(userId int) []freeInfo {
  141. m.lock.RLock()
  142. defer m.lock.RUnlock()
  143. ret, ok := m.userFreeInfo[userId]
  144. if !ok {
  145. return []freeInfo{}
  146. }
  147. return ret
  148. }
  149. func (m *userfreecount) getUserFreeCount(userId, matchId int) int {
  150. fi := m.getUserFreeInfo(userId)
  151. for _, v := range fi {
  152. if v.MatchId == matchId {
  153. return v.FreeCount
  154. }
  155. }
  156. return 0
  157. }