memcache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package archive
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "strconv"
  7. arcmdl "go-common/app/interface/main/creative/model/archive"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _prefix = "porder_"
  13. _cmPrefix = "arccm_"
  14. _addMidAndTitlePrefix = "add_midtitle_"
  15. )
  16. func limitMidSameTitle(mid int64, title string) string {
  17. ms := md5.Sum([]byte(title))
  18. return _addMidAndTitlePrefix + strconv.FormatInt(mid, 10) + "_" + hex.EncodeToString(ms[:])
  19. }
  20. func keyPorder(aid int64) string {
  21. return _prefix + strconv.FormatInt(aid, 10)
  22. }
  23. func keyArcCM(aid int64) string {
  24. return _cmPrefix + strconv.FormatInt(aid, 10)
  25. }
  26. // POrderCache get stat cache.
  27. func (d *Dao) POrderCache(c context.Context, aid int64) (st *arcmdl.Porder, err error) {
  28. var (
  29. conn = d.mc.Get(c)
  30. r *memcache.Item
  31. )
  32. defer conn.Close()
  33. r, err = conn.Get(keyPorder(aid))
  34. if err != nil {
  35. if err == memcache.ErrNotFound {
  36. err = nil
  37. } else {
  38. log.Error("conn.Get2(%d) error(%v)", aid, err)
  39. }
  40. return
  41. }
  42. if err = conn.Scan(r, &st); err != nil {
  43. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  44. st = nil
  45. }
  46. return
  47. }
  48. // AddPOrderCache add stat cache.
  49. func (d *Dao) AddPOrderCache(c context.Context, aid int64, st *arcmdl.Porder) (err error) {
  50. var (
  51. key = keyPorder(aid)
  52. )
  53. conn := d.mc.Get(c)
  54. defer conn.Close()
  55. if err = conn.Set(&memcache.Item{Key: key, Object: st, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  56. log.Error("memcache.Set(%v) error(%v)", key, err)
  57. }
  58. return
  59. }
  60. // ArcCMCache get stat cache.
  61. func (d *Dao) ArcCMCache(c context.Context, aid int64) (st *arcmdl.Commercial, err error) {
  62. var (
  63. conn = d.mc.Get(c)
  64. r *memcache.Item
  65. )
  66. defer conn.Close()
  67. r, err = conn.Get(keyArcCM(aid))
  68. if err != nil {
  69. if err == memcache.ErrNotFound {
  70. err = nil
  71. } else {
  72. log.Error("conn.Get2(%d) error(%v)", aid, err)
  73. }
  74. return
  75. }
  76. if err = conn.Scan(r, &st); err != nil {
  77. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  78. st = nil
  79. }
  80. return
  81. }
  82. // AddArcCMCache add stat cache.
  83. func (d *Dao) AddArcCMCache(c context.Context, aid int64, st *arcmdl.Commercial) (err error) {
  84. var (
  85. key = keyArcCM(aid)
  86. )
  87. conn := d.mc.Get(c)
  88. defer conn.Close()
  89. if err = conn.Set(&memcache.Item{Key: key, Object: st, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  90. log.Error("memcache.Set(%v) error(%v)", key, err)
  91. }
  92. return
  93. }
  94. // DelSubmitCache func
  95. func (d *Dao) DelSubmitCache(c context.Context, mid int64, title string) (err error) {
  96. conn := d.mc.Get(c)
  97. defer conn.Close()
  98. if err = conn.Delete(limitMidSameTitle(mid, title)); err == memcache.ErrNotFound {
  99. err = nil
  100. }
  101. return
  102. }