mc_limit.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package account
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "strconv"
  8. "time"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _addMidAndTitlePrefix = "add_midtitle_"
  14. _addMidHalfMinPrefix = "add_midhafmin_"
  15. )
  16. func limitMidHafMin(mid int64) string {
  17. return _addMidHalfMinPrefix + strconv.FormatInt(mid, 10)
  18. }
  19. func limitMidSameTitle(mid int64, title string) string {
  20. ms := md5.Sum([]byte(title))
  21. return _addMidAndTitlePrefix + strconv.FormatInt(mid, 10) + "_" + hex.EncodeToString(ms[:])
  22. }
  23. // HalfMin fn
  24. func (d *Dao) HalfMin(c context.Context, mid int64) (exist bool, ts uint64, err error) {
  25. var (
  26. conn = d.mc.Get(c)
  27. rp *memcache.Item
  28. )
  29. defer conn.Close()
  30. key := limitMidHafMin(mid)
  31. rp, err = conn.Get(key)
  32. if err != nil {
  33. if err == memcache.ErrNotFound {
  34. err = nil
  35. } else {
  36. log.Error("conn.Get error(%v) | key(%s) mid(%d)", err, key, mid)
  37. }
  38. return
  39. }
  40. if err = conn.Scan(rp, &ts); err != nil {
  41. log.Error("conn.Scan(%s) error(%v)", rp.Value, err)
  42. return
  43. }
  44. log.Info("HalfMin key(%s) ts(%d)", key, ts)
  45. if ts != 0 {
  46. exist = true
  47. }
  48. return
  49. }
  50. // AddHalfMin fn
  51. func (d *Dao) AddHalfMin(c context.Context, mid int64) (err error) {
  52. conn := d.mc.Get(c)
  53. defer conn.Close()
  54. key := limitMidHafMin(mid)
  55. ts := time.Now().Unix()
  56. if err = conn.Set(&memcache.Item{Key: key, Object: ts, Flags: memcache.FlagJSON, Expiration: d.mcLimitAddBasicExp}); err != nil {
  57. log.Error("memcache.set error(%v) | key(%s) mid(%d)", err, key, mid)
  58. }
  59. return
  60. }
  61. // DelHalfMin func
  62. func (d *Dao) DelHalfMin(c context.Context, mid int64) (err error) {
  63. conn := d.mc.Get(c)
  64. defer conn.Close()
  65. if err = conn.Delete(limitMidHafMin(mid)); err == memcache.ErrNotFound {
  66. err = nil
  67. }
  68. return
  69. }
  70. // SubmitCache get user submit cache.
  71. func (d *Dao) SubmitCache(c context.Context, mid int64, title string) (exist int8, err error) {
  72. var (
  73. conn = d.mc.Get(c)
  74. rp *memcache.Item
  75. )
  76. defer conn.Close()
  77. key := limitMidSameTitle(mid, title)
  78. rp, err = conn.Get(key)
  79. if err != nil {
  80. if err == memcache.ErrNotFound {
  81. err = nil
  82. } else {
  83. log.Error("conn.Get error(%v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
  84. }
  85. }
  86. if rp != nil {
  87. exist = int8(binary.BigEndian.Uint64(rp.Value))
  88. }
  89. return
  90. }
  91. // AddSubmitCache add submit cache into mc.
  92. func (d *Dao) AddSubmitCache(c context.Context, mid int64, title string) (err error) {
  93. conn := d.mc.Get(c)
  94. defer conn.Close()
  95. key := limitMidSameTitle(mid, title)
  96. bs := make([]byte, 8)
  97. binary.BigEndian.PutUint64(bs, 1)
  98. if err = conn.Set(&memcache.Item{Key: key, Object: bs, Flags: memcache.FlagJSON, Expiration: d.mcSubExp}); err != nil {
  99. log.Error("memcache.set error(%v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
  100. }
  101. return
  102. }
  103. // DelSubmitCache func
  104. func (d *Dao) DelSubmitCache(c context.Context, mid int64, title string) (err error) {
  105. conn := d.mc.Get(c)
  106. defer conn.Close()
  107. if err = conn.Delete(limitMidSameTitle(mid, title)); err == memcache.ErrNotFound {
  108. err = nil
  109. }
  110. return
  111. }
  112. func (d *Dao) pingMemcache(c context.Context) (err error) {
  113. conn := d.mc.Get(c)
  114. defer conn.Close()
  115. if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte("pong"), Expiration: 0}); err != nil {
  116. log.Error("mc.ping.Store error(%v)", err)
  117. return
  118. }
  119. return
  120. }