scoremanager_redis.go 781 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package slotscore
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/redis"
  5. "encoding/json"
  6. "time"
  7. )
  8. func getRedisKey() string {
  9. return "slotscore:userscores"
  10. }
  11. func (sc *scoremanager) loadUserScoreFromRedis() {
  12. data, ok := redis.String_Get(getRedisKey())
  13. if data == "" || !ok {
  14. return
  15. }
  16. sc.lock.Lock()
  17. err := json.Unmarshal([]byte(data), &sc.userscores)
  18. sc.lock.Unlock()
  19. if err != nil {
  20. log.Release("scoremanager.loadUserScoreFromRedis Unmarshal failed err:%v,%s", err, data)
  21. return
  22. }
  23. }
  24. func (sc *scoremanager) flush() {
  25. time.AfterFunc(refresh_config_sec*time.Second, sc.flush)
  26. sc.lock.RLock()
  27. if !sc.isDirty {
  28. sc.lock.RUnlock()
  29. return
  30. }
  31. sc.isDirty = false
  32. d, _ := json.Marshal(sc.userscores)
  33. sc.lock.RUnlock()
  34. go redis.String_Set(getRedisKey(), string(d))
  35. }