memcache.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package archive
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/service/main/archive/api"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _prefix = "a3p_"
  12. _prxfixSt = "stp_"
  13. )
  14. func keyArc(aid int64) string {
  15. return _prefix + strconv.FormatInt(aid, 10)
  16. }
  17. func keySt(aid int64) string {
  18. return _prxfixSt + strconv.FormatInt(aid, 10)
  19. }
  20. // statsCache get stat cache by aids
  21. func (d *Dao) statsCache(c context.Context, aids []int64) (cached map[int64]*api.Stat, missed []int64, err error) {
  22. cached = make(map[int64]*api.Stat, len(aids))
  23. var (
  24. conn = d.mc.Get(c)
  25. keys = make([]string, 0, len(aids))
  26. rs map[string]*memcache.Item
  27. )
  28. defer conn.Close()
  29. for _, aid := range aids {
  30. keys = append(keys, keySt(aid))
  31. }
  32. if rs, err = conn.GetMulti(keys); err != nil {
  33. err = errors.Wrapf(err, "%v", keys)
  34. return
  35. }
  36. for _, item := range rs {
  37. var st = &api.Stat{}
  38. if err = conn.Scan(item, st); err != nil {
  39. log.Error("conn.Scan(%s) error(%v)", item.Value, err)
  40. err = nil
  41. continue
  42. }
  43. cached[st.Aid] = st
  44. }
  45. if len(cached) == len(aids) {
  46. return
  47. }
  48. for _, aid := range aids {
  49. if _, ok := cached[aid]; !ok {
  50. missed = append(missed, aid)
  51. }
  52. }
  53. return
  54. }
  55. // arcsCache get archives cache.
  56. func (d *Dao) arcsCache(c context.Context, aids []int64) (cached map[int64]*api.Arc, missed []int64, err error) {
  57. var (
  58. keys = make([]string, 0, len(aids))
  59. conn = d.mc.Get(c)
  60. aidmap = make(map[string]int64, len(aids))
  61. rs map[string]*memcache.Item
  62. a *api.Arc
  63. )
  64. cached = make(map[int64]*api.Arc, len(aids))
  65. defer conn.Close()
  66. for _, aid := range aids {
  67. k := keyArc(aid)
  68. if _, ok := aidmap[k]; !ok {
  69. keys = append(keys, k)
  70. aidmap[k] = aid
  71. }
  72. }
  73. if rs, err = conn.GetMulti(keys); err != nil {
  74. err = errors.Wrapf(err, "%v", keys)
  75. return
  76. }
  77. for k, r := range rs {
  78. a = &api.Arc{}
  79. if err = conn.Scan(r, a); err != nil {
  80. log.Error("conn.Scan(%s) error(%v)", r.Value, err)
  81. err = nil
  82. continue
  83. }
  84. cached[aidmap[k]] = a
  85. // delete hit key
  86. delete(aidmap, k)
  87. }
  88. // missed key
  89. missed = make([]int64, 0, len(aidmap))
  90. for _, aid := range aidmap {
  91. missed = append(missed, aid)
  92. }
  93. return
  94. }