itemmgr_sys.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package handler
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. "bet24.com/log"
  7. pb "bet24.com/servers/micros/item_inventory/proto"
  8. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  9. )
  10. const config_key = "item_config"
  11. const refresh_config_sec = 600
  12. func (im *itemmgr) loadSysItemFromJson() bool {
  13. time.AfterFunc(refresh_config_sec*time.Second, func() {
  14. im.loadSysItemFromJson()
  15. })
  16. configString := platformconfig.GetConfig(config_key)
  17. if configString == "" {
  18. data, err := os.ReadFile("serviceconf/systemitems.json")
  19. if err != nil {
  20. return false
  21. }
  22. configString = string(data)
  23. platformconfig.SetConfig(config_key, configString)
  24. } else {
  25. log.Release("item.itemmgr loading config from redis")
  26. }
  27. if configString == im.lastConfig {
  28. return false
  29. }
  30. im.lastConfig = configString
  31. var list []pb.Item
  32. err := json.Unmarshal([]byte(configString), &list)
  33. if err != nil {
  34. log.Release("itemmgr.loadSysItemFromJson Unmarshal item failed err:%v", err)
  35. return false
  36. }
  37. if len(list) == 0 {
  38. log.Release("itemmgr.loadSysItemFromJson Unmarshal item failed err:%v", err)
  39. return false
  40. }
  41. im.lock.Lock()
  42. for k, v := range list {
  43. im.itemlist[v.Id] = &list[k]
  44. }
  45. im.lock.Unlock()
  46. return true
  47. }