creation_mc.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "strconv"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _subPrefix = "artsl_"
  13. )
  14. func midSub(mid int64, title string) string {
  15. ms := md5.Sum([]byte(title))
  16. return _subPrefix + strconv.FormatInt(mid, 10) + "_" + hex.EncodeToString(ms[:])
  17. }
  18. // SubmitCache get user submit cache.
  19. func (d *Dao) SubmitCache(c context.Context, mid int64, title string) (exist bool, err error) {
  20. conn := d.mc.Get(c)
  21. defer conn.Close()
  22. key := midSub(mid, title)
  23. _, err = conn.Get(key)
  24. if err != nil {
  25. if err == memcache.ErrNotFound {
  26. err = nil
  27. } else {
  28. log.Error("conn.Get error(%+v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
  29. PromError("creation:获取标题缓存")
  30. }
  31. return
  32. }
  33. exist = true
  34. return
  35. }
  36. // AddSubmitCache add submit cache into mc.
  37. func (d *Dao) AddSubmitCache(c context.Context, mid int64, title string) (err error) {
  38. conn := d.mc.Get(c)
  39. defer conn.Close()
  40. key := midSub(mid, title)
  41. bs := make([]byte, 8)
  42. binary.BigEndian.PutUint64(bs, 1)
  43. if err = conn.Set(&memcache.Item{Key: key, Object: bs, Flags: memcache.FlagJSON, Expiration: d.mcSubExp}); err != nil {
  44. log.Error("memcache.set error(%+v) | key(%s) mid(%d) title(%s)", err, key, mid, title)
  45. PromError("creation:设定标题缓存")
  46. }
  47. return
  48. }
  49. // DelSubmitCache del submit cache into mc.
  50. func (d *Dao) DelSubmitCache(c context.Context, mid int64, title string) (err error) {
  51. conn := d.mc.Get(c)
  52. defer conn.Close()
  53. if err = conn.Delete(midSub(mid, title)); err == memcache.ErrNotFound {
  54. err = nil
  55. }
  56. if err != nil {
  57. PromError("creation:删除标题缓存")
  58. log.Error("creation: dao.DelSubmitCache(mid: %v, title: %v) err: %+v", mid, title, err)
  59. }
  60. return
  61. }