platformconfig.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package handler
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/redis"
  5. pb "bet24.com/servers/micros/platformconfig/proto"
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. var instance *PlatformConfig
  12. func GetInstance() *PlatformConfig {
  13. if instance == nil {
  14. instance = newHandler()
  15. }
  16. return instance
  17. }
  18. func Dump(cmd, param1, param2 string) {
  19. GetInstance().dump(cmd, param1, param2)
  20. }
  21. func newHandler() *PlatformConfig {
  22. ret := new(PlatformConfig)
  23. ret.ctor()
  24. return ret
  25. }
  26. type PlatformConfig struct{}
  27. func (h *PlatformConfig) ctor() {
  28. }
  29. func (d *PlatformConfig) dump(cmd, param1, param2 string) {
  30. //log.Debug("DbEngine.Dump %s,%s,%s", cmd, param1, param2)
  31. if cmd == "history" {
  32. getHistoryInstance().dump(param1, param2)
  33. return
  34. }
  35. if cmd == "" {
  36. d.dumpKeys()
  37. return
  38. }
  39. log.Release("-------------------------------")
  40. defer func() {
  41. log.Release("+++++++++++++++++++++++++++++++")
  42. log.Release("")
  43. }()
  44. log.Release(" platformconfig key[%s] = %s", cmd, d.getConfig(cmd))
  45. }
  46. const redis_key = "platformconfig"
  47. const cacheUpdateSecond = 60
  48. var keyscache struct {
  49. keys []string
  50. lastUpdate int64
  51. }
  52. func getRedisKey(key string) string {
  53. return fmt.Sprintf("%s:%s", redis_key, key)
  54. }
  55. func (h *PlatformConfig) getConfig(key string) string {
  56. ret, ok := redis.String_Get(getRedisKey(key))
  57. if !ok {
  58. return ""
  59. }
  60. return ret
  61. }
  62. func (h *PlatformConfig) setConfig(key, value string) {
  63. if !json.Valid([]byte(value)) {
  64. log.Debug("platformconfig.setConfig invalid data:%s", value)
  65. return
  66. }
  67. oldValue := h.getConfig(key)
  68. redis.String_Set(getRedisKey(key), value)
  69. getHistoryInstance().addBackup(key, oldValue, value)
  70. // 广播一个通知
  71. var d redis.Channel_msg
  72. d.Message = pb.NOTIFY_MESSAGE
  73. d.Data = key
  74. js, _ := json.Marshal(d)
  75. redis.Publish(string(js))
  76. }
  77. func getCopyList(src []string) []string {
  78. var ret []string
  79. if len(src) > 0 {
  80. ret = make([]string, len(src))
  81. copy(ret, src)
  82. }
  83. return ret
  84. }
  85. func (h *PlatformConfig) getConfigList() []string {
  86. now := time.Now().Unix()
  87. if now-keyscache.lastUpdate < cacheUpdateSecond {
  88. return getCopyList(keyscache.keys)
  89. }
  90. keyscache.keys = []string{}
  91. keys := redis.Key_GetKeys(fmt.Sprintf("%s:*", redis_key))
  92. for _, v := range keys {
  93. parts := strings.Split(v, ":")
  94. if len(parts) > 1 {
  95. keyscache.keys = append(keyscache.keys, parts[1])
  96. }
  97. }
  98. keyscache.lastUpdate = now
  99. return getCopyList(keyscache.keys)
  100. }
  101. func (h *PlatformConfig) dumpKeys() {
  102. log.Release("-------------------------------")
  103. defer func() {
  104. log.Release("+++++++++++++++++++++++++++++++")
  105. log.Release("")
  106. }()
  107. keys := h.getConfigList()
  108. log.Release("platformconfig.dumpKeys count [%d], lastUpdate past %d", len(keys), time.Now().Unix()-keyscache.lastUpdate)
  109. for _, v := range keys {
  110. log.Release(" %s", v)
  111. }
  112. }