memcache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/dynamic/model"
  5. gmc "go-common/library/cache/memcache"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _keyRegionArcs = "dyra" // key of region archives
  10. _keyRegionTagArcs = "dyrta" // key of tag archives
  11. )
  12. // pingMC ping memcache.
  13. func (d *Dao) pingMC(c context.Context) (err error) {
  14. conn := d.mc.Get(c)
  15. if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}); err != nil {
  16. log.Error("conn.Store(set, ping, 1) error(%v)", err)
  17. }
  18. conn.Close()
  19. return
  20. }
  21. // SetRegionCache set region archive to cache.
  22. func (d *Dao) SetRegionCache(c context.Context, regionArcs map[int32][]int64) (err error) {
  23. conn := d.mc.Get(c)
  24. defer conn.Close()
  25. tmp := make(map[int32]*model.Aids)
  26. for k, v := range regionArcs {
  27. tmp[k] = &model.Aids{IDs: v}
  28. }
  29. item := &gmc.Item{Key: _keyRegionArcs, Object: &model.Region{Aids: tmp}, Expiration: d.mcExpire, Flags: gmc.FlagProtobuf}
  30. if err = conn.Set(item); err != nil {
  31. log.Error("SetRegionCache error(%v)", err)
  32. }
  33. return
  34. }
  35. // RegionCache get region archive from cache.
  36. func (d *Dao) RegionCache(c context.Context) (rs map[int32][]int64) {
  37. conn := d.mc.Get(c)
  38. defer conn.Close()
  39. res, err := conn.Get(_keyRegionArcs)
  40. if err != nil {
  41. if err == gmc.ErrNotFound {
  42. err = nil
  43. } else {
  44. log.Error("conn.Get(%d) error(%v)", _keyRegionArcs, err)
  45. }
  46. return
  47. }
  48. rc := &model.Region{}
  49. if err = conn.Scan(res, rc); err != nil {
  50. log.Error("conn.Scan error(%v)", err)
  51. return
  52. }
  53. rs = make(map[int32][]int64)
  54. for k, v := range rc.Aids {
  55. rs[k] = v.IDs
  56. }
  57. return
  58. }
  59. // SetTagCache set region tag archvie to cache.
  60. func (d *Dao) SetTagCache(c context.Context, regionTagArcs map[string][]int64) (err error) {
  61. conn := d.mc.Get(c)
  62. defer conn.Close()
  63. tmp := make(map[string]*model.Aids)
  64. for k, v := range regionTagArcs {
  65. tmp[k] = &model.Aids{IDs: v}
  66. }
  67. item := &gmc.Item{Key: _keyRegionTagArcs, Object: &model.Tag{Aids: tmp}, Expiration: d.mcExpire, Flags: gmc.FlagProtobuf}
  68. if err = conn.Set(item); err != nil {
  69. log.Error("SetRegionCache error(%v)", err)
  70. }
  71. return
  72. }
  73. // TagCache get region tag archive from cache.
  74. func (d *Dao) TagCache(c context.Context) (rs map[string][]int64) {
  75. conn := d.mc.Get(c)
  76. defer conn.Close()
  77. res, err := conn.Get(_keyRegionTagArcs)
  78. if err != nil {
  79. if err == gmc.ErrNotFound {
  80. err = nil
  81. } else {
  82. log.Error("conn.Get(%d) error(%v)", _keyRegionTagArcs, err)
  83. }
  84. return
  85. }
  86. tc := &model.Tag{}
  87. if err = conn.Scan(res, tc); err != nil {
  88. log.Error("conn.Scan error(%v)", err)
  89. return
  90. }
  91. rs = make(map[string][]int64)
  92. for k, v := range tc.Aids {
  93. rs[k] = v.IDs
  94. }
  95. return
  96. }