memcache.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package recommend
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-card/model/card/ai"
  5. "go-common/library/cache/memcache"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _prefixRcmdAids = "rc"
  10. _prefixRcmd = "rc2"
  11. _prefixFollowModeList = "fml"
  12. )
  13. func keyRcmdAids() string {
  14. return _prefixRcmdAids
  15. }
  16. func keyRcmd() string {
  17. return _prefixRcmd
  18. }
  19. func keyFollowModeList() string {
  20. return _prefixFollowModeList
  21. }
  22. // AddRcmdCache add ai into cahce.
  23. func (d *Dao) AddRcmdAidsCache(c context.Context, aids []int64) (err error) {
  24. conn := d.mc.Get(c)
  25. key := keyRcmdAids()
  26. item := &memcache.Item{Key: key, Object: aids, Flags: memcache.FlagJSON, Expiration: d.expireMc}
  27. if err = conn.Set(item); err != nil {
  28. err = errors.Wrapf(err, "%v", aids)
  29. }
  30. conn.Close()
  31. return
  32. }
  33. // RcmdCache get ai cache data from cache
  34. func (d *Dao) RcmdAidsCache(c context.Context) (aids []int64, err error) {
  35. var r *memcache.Item
  36. conn := d.mc.Get(c)
  37. key := keyRcmdAids()
  38. defer conn.Close()
  39. if r, err = conn.Get(key); err != nil {
  40. if err == memcache.ErrNotFound {
  41. err = nil
  42. return
  43. }
  44. err = errors.Wrap(err, key)
  45. return
  46. }
  47. if err = conn.Scan(r, &aids); err != nil {
  48. err = errors.Wrapf(err, "%s", r.Value)
  49. }
  50. return
  51. }
  52. // AddRcmdCache add ai into cahce.
  53. func (d *Dao) AddRcmdCache(c context.Context, is []*ai.Item) (err error) {
  54. conn := d.mc.Get(c)
  55. key := keyRcmd()
  56. item := &memcache.Item{Key: key, Object: is, Flags: memcache.FlagJSON, Expiration: d.expireMc}
  57. if err = conn.Set(item); err != nil {
  58. err = errors.Wrap(err, key)
  59. }
  60. conn.Close()
  61. return
  62. }
  63. // RcmdCache get ai cache data from cache
  64. func (d *Dao) RcmdCache(c context.Context) (is []*ai.Item, err error) {
  65. var r *memcache.Item
  66. conn := d.mc.Get(c)
  67. key := keyRcmd()
  68. defer conn.Close()
  69. if r, err = conn.Get(key); err != nil {
  70. if err == memcache.ErrNotFound {
  71. err = nil
  72. return
  73. }
  74. err = errors.Wrap(err, key)
  75. return
  76. }
  77. if err = conn.Scan(r, &is); err != nil {
  78. err = errors.Wrapf(err, "%s", r.Value)
  79. }
  80. return
  81. }
  82. // AddFollowModeListCache is.
  83. func (d *Dao) AddFollowModeListCache(c context.Context, list map[int64]struct{}) (err error) {
  84. conn := d.mc.Get(c)
  85. key := keyFollowModeList()
  86. item := &memcache.Item{Key: key, Object: list, Flags: memcache.FlagJSON, Expiration: d.expireMc}
  87. if err = conn.Set(item); err != nil {
  88. err = errors.Wrap(err, key)
  89. }
  90. conn.Close()
  91. return
  92. }
  93. // FollowModeListCache is.
  94. func (d *Dao) FollowModeListCache(c context.Context) (list map[int64]struct{}, err error) {
  95. var r *memcache.Item
  96. conn := d.mc.Get(c)
  97. key := keyFollowModeList()
  98. defer conn.Close()
  99. if r, err = conn.Get(key); err != nil {
  100. if err == memcache.ErrNotFound {
  101. err = nil
  102. return
  103. }
  104. err = errors.Wrap(err, key)
  105. return
  106. }
  107. if err = conn.Scan(r, &list); err != nil {
  108. err = errors.Wrapf(err, "%s", r.Value)
  109. }
  110. return
  111. }