dynamic_redis.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/service/main/archive/api"
  7. dymdl "go-common/app/service/main/dynamic/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _keyRegion = "dr_"
  13. _keyRegionTag = "drt_"
  14. _keyRegions = "drs_"
  15. _keyRegionFmt = "%d_%d_%d"
  16. _keyRegionTagFmt = "%d_%d_%d_%d"
  17. )
  18. func keyRegion(rid int32, pn, ps int) string {
  19. return _keyRegion + fmt.Sprintf(_keyRegionFmt, rid, pn, ps)
  20. }
  21. func keyRegionTag(tagID int64, rid int32, pn, ps int) string {
  22. return _keyRegionTag + fmt.Sprintf(_keyRegionTagFmt, tagID, rid, pn, ps)
  23. }
  24. // SetRegionBakCache set dynamic region data to cache.
  25. func (d *Dao) SetRegionBakCache(c context.Context, rid int32, pn, ps int, rs *dymdl.DynamicArcs3) (err error) {
  26. key := keyRegion(rid, pn, ps)
  27. err = d.setBakCache(c, key, rs)
  28. return
  29. }
  30. // RegionBakCache get dynamic region from cache.
  31. func (d *Dao) RegionBakCache(c context.Context, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
  32. d.cacheProm.Incr("dynamic_region_remote_cache")
  33. key := keyRegion(rid, pn, ps)
  34. rs, err = d.bakCache(c, key)
  35. if rs == nil {
  36. log.Error("RegionBakCache d.bakCache(%d,%d,%d,%s) is nill", rid, pn, ps, key)
  37. }
  38. return
  39. }
  40. // SetRegionTagBakCache set dynamic region tag data to cache.
  41. func (d *Dao) SetRegionTagBakCache(c context.Context, tagID int64, rid int32, pn, ps int, rs *dymdl.DynamicArcs3) (err error) {
  42. key := keyRegionTag(tagID, rid, pn, ps)
  43. err = d.setBakCache(c, key, rs)
  44. return
  45. }
  46. // RegionTagBakCache get dynamic region tag from cache.
  47. func (d *Dao) RegionTagBakCache(c context.Context, tagID int64, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
  48. d.cacheProm.Incr("dynamic_tag_remote_cache")
  49. key := keyRegionTag(tagID, rid, pn, ps)
  50. rs, err = d.bakCache(c, key)
  51. if rs == nil {
  52. log.Error("RegionTagBakCache d.bakCache(%d,%d,%d,%s) is nill", rid, pn, ps, key)
  53. }
  54. return
  55. }
  56. // SetRegionsBakCache set regions data to cache.
  57. func (d *Dao) SetRegionsBakCache(c context.Context, rs map[int32][]*api.Arc) (err error) {
  58. key := _keyRegions
  59. conn := d.redisBak.Get(c)
  60. defer conn.Close()
  61. var bs []byte
  62. if bs, err = json.Marshal(rs); err != nil {
  63. log.Error("json.Marshal(%v) error(%v)", rs, err)
  64. return
  65. }
  66. if err = conn.Send("SET", key, bs); err != nil {
  67. log.Error("conn.Send(SET,%s,%s) error(%v)", key, string(bs), err)
  68. return
  69. }
  70. if err = conn.Send("EXPIRE", key, d.redisDynamicBakExpire); err != nil {
  71. log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisDynamicBakExpire, err)
  72. return
  73. }
  74. if err = conn.Flush(); err != nil {
  75. log.Error("conn.Flush error(%v)", err)
  76. return
  77. }
  78. for i := 0; i < 2; i++ {
  79. if _, err = conn.Receive(); err != nil {
  80. log.Error("conn.Recevie(%d) error(%v0", i, err)
  81. }
  82. }
  83. return
  84. }
  85. // RegionsBakCache get dynamic region data from cache.
  86. func (d *Dao) RegionsBakCache(c context.Context) (rs map[int32][]*api.Arc, err error) {
  87. d.cacheProm.Incr("dynamic_regions_remote_cache")
  88. conn := d.redisBak.Get(c)
  89. defer conn.Close()
  90. var values []byte
  91. if values, err = redis.Bytes(conn.Do("GET", _keyRegions)); err != nil {
  92. if err == redis.ErrNil {
  93. err = nil
  94. log.Error("RegionsBakCache (%s) return nil ", _keyRegions)
  95. } else {
  96. log.Error("conn.Do(GET,%s) error(%v)", _keyRegions, err)
  97. }
  98. return
  99. }
  100. rs = make(map[int32][]*api.Arc)
  101. if err = json.Unmarshal(values, &rs); err != nil {
  102. log.Error("json.Unmarshal(%v) error(%v)", values, err)
  103. }
  104. return
  105. }
  106. func (d *Dao) setBakCache(c context.Context, key string, rs *dymdl.DynamicArcs3) (err error) {
  107. conn := d.redisBak.Get(c)
  108. defer conn.Close()
  109. var bs []byte
  110. if bs, err = json.Marshal(rs); err != nil {
  111. log.Error("json.Marshal(%v) error(%v)", rs, err)
  112. return
  113. }
  114. if err = conn.Send("SET", key, bs); err != nil {
  115. log.Error("conn.Send(SET,%s,%s) error(%v)", key, string(bs), err)
  116. return
  117. }
  118. if err = conn.Send("EXPIRE", key, d.redisDynamicBakExpire); err != nil {
  119. log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisDynamicBakExpire, err)
  120. return
  121. }
  122. if err = conn.Flush(); err != nil {
  123. log.Error("conn.Flush error(%v)", err)
  124. return
  125. }
  126. for i := 0; i < 2; i++ {
  127. if _, err = conn.Receive(); err != nil {
  128. log.Error("conn.Receive(%d) error(%v)", i, err)
  129. }
  130. }
  131. return
  132. }
  133. func (d *Dao) bakCache(c context.Context, key string) (rs *dymdl.DynamicArcs3, err error) {
  134. conn := d.redisBak.Get(c)
  135. defer conn.Close()
  136. var values []byte
  137. if values, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  138. if err == redis.ErrNil {
  139. err = nil
  140. } else {
  141. log.Error("conn.Do(GET,%s) error(%v)", key, err)
  142. }
  143. return
  144. }
  145. rs = &dymdl.DynamicArcs3{}
  146. if err = json.Unmarshal(values, rs); err != nil {
  147. log.Error("json.Unmarshal(%v) error(%v)", values, err)
  148. }
  149. return
  150. }