case_mc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/interface/main/credit/model"
  6. gmc "go-common/library/cache/memcache"
  7. )
  8. const (
  9. _prefixCaseInfo = "ca_in_%d"
  10. _prefixVoteCaseInfo = "vc_in_%d_%d"
  11. _caseVoteTop = "ca_vo_top_%d"
  12. )
  13. func caseInfoKey(cid int64) string {
  14. return fmt.Sprintf(_prefixCaseInfo, cid)
  15. }
  16. func voteCaseInfoKey(mid int64, cid int64) string {
  17. return fmt.Sprintf(_prefixVoteCaseInfo, mid, cid)
  18. }
  19. func caseVoteTopKey(mid int64) string {
  20. return fmt.Sprintf(_caseVoteTop, mid)
  21. }
  22. // SetCaseInfoCache set case info.
  23. func (d *Dao) SetCaseInfoCache(c context.Context, cid int64, bc *model.BlockedCase) (err error) {
  24. var (
  25. conn = d.mc.Get(c)
  26. item = &gmc.Item{Key: caseInfoKey(cid), Object: bc, Expiration: d.minCommonExpire, Flags: gmc.FlagJSON}
  27. )
  28. defer conn.Close()
  29. err = conn.Set(item)
  30. return
  31. }
  32. // CaseInfoCache get case info cache
  33. func (d *Dao) CaseInfoCache(c context.Context, cid int64) (bc *model.BlockedCase, err error) {
  34. var (
  35. reply *gmc.Item
  36. conn = d.mc.Get(c)
  37. )
  38. defer conn.Close()
  39. if reply, err = conn.Get(caseInfoKey(cid)); err != nil {
  40. if err == gmc.ErrNotFound {
  41. err = nil
  42. }
  43. return
  44. }
  45. bc = &model.BlockedCase{}
  46. err = conn.Scan(reply, &bc)
  47. return
  48. }
  49. // SetVoteInfoCache set vote case info.
  50. func (d *Dao) SetVoteInfoCache(c context.Context, mid, cid int64, vi *model.VoteInfo) (err error) {
  51. var (
  52. conn = d.mc.Get(c)
  53. item = &gmc.Item{Key: voteCaseInfoKey(mid, cid), Object: vi, Expiration: d.userExpire, Flags: gmc.FlagJSON}
  54. )
  55. defer conn.Close()
  56. err = conn.Set(item)
  57. return
  58. }
  59. // VoteInfoCache get vote case info cache
  60. func (d *Dao) VoteInfoCache(c context.Context, mid, cid int64) (vi *model.VoteInfo, err error) {
  61. var (
  62. reply *gmc.Item
  63. conn = d.mc.Get(c)
  64. )
  65. defer conn.Close()
  66. reply, err = conn.Get(voteCaseInfoKey(mid, cid))
  67. if err != nil {
  68. if err == gmc.ErrNotFound {
  69. err = nil
  70. }
  71. return
  72. }
  73. vi = &model.VoteInfo{}
  74. err = conn.Scan(reply, &vi)
  75. return
  76. }
  77. // CaseVoteTopCache get case votes by top 100.
  78. func (d *Dao) CaseVoteTopCache(c context.Context, mid int64) (bs []*model.BlockedCase, err error) {
  79. var (
  80. reply *gmc.Item
  81. conn = d.mc.Get(c)
  82. )
  83. defer conn.Close()
  84. reply, err = conn.Get(caseVoteTopKey(mid))
  85. if err != nil {
  86. if err == gmc.ErrNotFound {
  87. err = nil
  88. }
  89. return
  90. }
  91. bs = make([]*model.BlockedCase, 0)
  92. err = conn.Scan(reply, &bs)
  93. return
  94. }
  95. // SetCaseVoteTopCache set case votes by top 100.
  96. func (d *Dao) SetCaseVoteTopCache(c context.Context, mid int64, bs []*model.BlockedCase) (err error) {
  97. var (
  98. conn = d.mc.Get(c)
  99. item = &gmc.Item{Key: caseVoteTopKey(mid), Object: bs, Expiration: d.userExpire, Flags: gmc.FlagJSON}
  100. )
  101. defer conn.Close()
  102. err = conn.Set(item)
  103. return
  104. }