confkv.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. v1pb "go-common/app/service/live/resource/api/grpc/v1"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _findConfkvSQL = "SELECT `value` FROM `confkv` WHERE `key` = ?"
  11. _addConfkvSQL = "INSERT INTO `confkv` (`key`,`value`) VALUES (?,?)"
  12. _updateConfkvSQL = "UPDATE `confkv` SET `value`=? WHERE `key`=?"
  13. )
  14. const (
  15. _confLiveCheck = "live_check"
  16. _platformAndroid = "android"
  17. _platformIos = "ios"
  18. )
  19. // GetLiveCheck live.app-interface call
  20. // cache -> db
  21. func (d *Dao) GetLiveCheck(c context.Context, platform, system, mobile string) (isLive int64) {
  22. isLive = int64(1)
  23. inst := 0
  24. res, ok := d.sCache[inst].Get(cacheLiveCheckKey(platform, system, mobile))
  25. if !ok {
  26. value, err := d.ConfKv(c, _confLiveCheck)
  27. if err != nil {
  28. log.Error("[LiveCheck] get live check error by from source")
  29. return
  30. }
  31. if value == "" {
  32. log.Error("[LiveCheck] get live check error by source data empty")
  33. return
  34. }
  35. list := &v1pb.GetLiveCheckListResp{}
  36. err = json.Unmarshal([]byte(value), list)
  37. if err != nil {
  38. log.Error("[LiveCheck] get live check error by source data wrong format")
  39. return
  40. }
  41. log.Info("[LiveCheck] live_check list is %v", list)
  42. switch platform {
  43. case _platformAndroid:
  44. for _, v := range list.Android {
  45. if v.System == system {
  46. for _, m := range v.Mobile {
  47. if m == mobile {
  48. isLive = int64(0)
  49. }
  50. }
  51. }
  52. }
  53. case _platformIos:
  54. for _, v := range list.Ios {
  55. if v.System == system {
  56. for _, m := range v.Mobile {
  57. log.Info("[LiveCheck] range m %v mobile %v", m, mobile)
  58. if m == mobile {
  59. isLive = int64(0)
  60. }
  61. }
  62. }
  63. }
  64. }
  65. d.sCache[inst].Put(cacheLiveCheckKey(platform, system, mobile), isLive)
  66. return
  67. }
  68. isLive = res.(int64)
  69. return
  70. }
  71. // ConfKv get data from cache if miss will call source method, then add to cache.
  72. func (d *Dao) ConfKv(c context.Context, key string) (value string, err error) {
  73. inst := 0
  74. res, ok := d.sCache[inst].Get(cacheConfKey(key))
  75. if !ok {
  76. log.Info("[LiveCheck] conf cache miss")
  77. value, err = d.RawConfKv(c, key)
  78. if err != nil {
  79. return
  80. }
  81. d.sCache[inst].Put(cacheConfKey(key), value)
  82. return
  83. }
  84. log.Info("[LiveCheck] conf cache hit")
  85. value = res.(string)
  86. return
  87. }
  88. // SetLiveCheck set live_check conf
  89. func (d *Dao) SetLiveCheck(c context.Context, value string) (err error) {
  90. err = d.AddOrUpdateConfKv(c, _confLiveCheck, value)
  91. return
  92. }
  93. // GetLiveCheckList get live_check conf
  94. func (d *Dao) GetLiveCheckList(c context.Context) (value string, err error) {
  95. value, err = d.RawConfKv(c, _confLiveCheck)
  96. return
  97. }
  98. // RawConfKv get conf
  99. func (d *Dao) RawConfKv(c context.Context, key string) (value string, err error) {
  100. row := d.db.QueryRow(c, _findConfkvSQL, key)
  101. if err = row.Scan(&value); err != nil {
  102. if err == sql.ErrNoRows {
  103. err = nil
  104. return
  105. }
  106. log.Error("[SelectConfKv] row.Scan() error(%v)", err)
  107. }
  108. return
  109. }
  110. // AddOrUpdateConfKv add or update conf
  111. func (d *Dao) AddOrUpdateConfKv(c context.Context, key string, value string) (err error) {
  112. oldValue, err := d.RawConfKv(c, key)
  113. if err != nil {
  114. return
  115. }
  116. if oldValue != "" {
  117. //update
  118. log.Info("[LiveCheck] update db value %v", value)
  119. if _, err = d.db.Exec(c, _updateConfkvSQL, value, key); err != nil {
  120. log.Error("[AddOrUpdateConfKv] UpdateConfKv:db.Exec(%v,$v) error(%v)", key, value, err)
  121. }
  122. return
  123. }
  124. //add
  125. log.Info("[LiveCheck] add db value %v", value)
  126. if _, err = d.db.Exec(c, _addConfkvSQL, key, value); err != nil {
  127. log.Error("[AddOrUpdateConfKv] AddConfKv:db.Exec(%v,$v) error(%v)", key, value, err)
  128. }
  129. return
  130. }