freemgr.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package dailywheel
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/redis"
  5. "encoding/json"
  6. "sync"
  7. "time"
  8. )
  9. const redis_key = "dailywheel_freeinfo"
  10. var fm *freemgr
  11. type freemgr struct {
  12. lockUser *sync.RWMutex
  13. lockItem *sync.RWMutex
  14. UserFreeInfo map[int]int
  15. ItemResultCount map[int]int
  16. LastDay int
  17. isDirty bool
  18. }
  19. func getFreeMgr() *freemgr {
  20. if fm == nil {
  21. fm = new(freemgr)
  22. fm.ctor()
  23. }
  24. return fm
  25. }
  26. func (fm *freemgr) ctor() {
  27. fm.lockUser = &sync.RWMutex{}
  28. fm.lockItem = &sync.RWMutex{}
  29. fm.UserFreeInfo = make(map[int]int)
  30. fm.ItemResultCount = make(map[int]int)
  31. fm.LastDay = time.Now().Day()
  32. fm.loadFreeInfo()
  33. time.AfterFunc(10*time.Minute, fm.saveFreeInfo)
  34. }
  35. func (fm *freemgr) checkDayChanged() {
  36. nowDay := time.Now().Day()
  37. if nowDay == fm.LastDay {
  38. return
  39. }
  40. fm.isDirty = true
  41. fm.LastDay = nowDay
  42. fm.lockUser.Lock()
  43. fm.UserFreeInfo = make(map[int]int)
  44. fm.lockUser.Unlock()
  45. fm.lockItem.Lock()
  46. fm.ItemResultCount = make(map[int]int)
  47. fm.lockItem.Unlock()
  48. }
  49. func (fm *freemgr) isFree(userId int) bool {
  50. fm.checkDayChanged()
  51. fm.lockUser.RLock()
  52. defer fm.lockUser.RUnlock()
  53. count, ok := fm.UserFreeInfo[userId]
  54. if !ok || count == 0 {
  55. return true
  56. }
  57. return false
  58. }
  59. func (fm *freemgr) useFree(userId int) bool {
  60. if !fm.isFree(userId) {
  61. return false
  62. }
  63. fm.lockUser.Lock()
  64. fm.UserFreeInfo[userId] = 1
  65. fm.lockUser.Unlock()
  66. fm.isDirty = true
  67. return true
  68. }
  69. func (fm *freemgr) addItemResult(idx int, maxCount int) {
  70. if maxCount == 0 {
  71. return
  72. }
  73. fm.lockItem.Lock()
  74. fm.ItemResultCount[idx]++
  75. fm.lockItem.Unlock()
  76. fm.isDirty = true
  77. }
  78. func (fm *freemgr) getItemResultCount(idx int) int {
  79. fm.lockItem.RLock()
  80. c, ok := fm.ItemResultCount[idx]
  81. fm.lockItem.RUnlock()
  82. if !ok {
  83. return 0
  84. }
  85. return c
  86. }
  87. func (fm *freemgr) isItemAvailable(idx int, maxCount int) bool {
  88. if maxCount == 0 {
  89. return true
  90. }
  91. fm.checkDayChanged()
  92. return fm.getItemResultCount(idx) < maxCount
  93. }
  94. func (fm *freemgr) loadFreeInfo() {
  95. data, ok := redis.String_Get(redis_key)
  96. if data == "" || !ok {
  97. return
  98. }
  99. err := json.Unmarshal([]byte(data), &fm)
  100. if err != nil {
  101. log.Release("freemgr.loadFreeInfo Unmarshal faied %s", data)
  102. return
  103. }
  104. fm.checkDayChanged()
  105. }
  106. func (fm *freemgr) saveFreeInfo() {
  107. time.AfterFunc(10*time.Minute, fm.saveFreeInfo)
  108. if !fm.isDirty {
  109. return
  110. }
  111. d, _ := json.Marshal(fm)
  112. redis.String_SetEx(redis_key, string(d), 86400)
  113. }
  114. func (fm *freemgr) dump(param string) {
  115. log.Release("-------------------------------")
  116. log.Release("freemgr.dump %s", param)
  117. defer func() {
  118. log.Release("+++++++++++++++++++++++++++++++")
  119. log.Release("")
  120. }()
  121. d, _ := json.Marshal(fm)
  122. log.Release(string(d))
  123. }