memcache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _prefixUUID = "uuid_%d_%s"
  10. _prefixCD = "cd_%d_%d"
  11. )
  12. func uuidKey(biz int64, uuid string) string {
  13. return fmt.Sprintf(_prefixUUID, biz, uuid)
  14. }
  15. func cdKey(app, mid int64) string {
  16. return fmt.Sprintf(_prefixCD, app, mid)
  17. }
  18. // pingMc ping memcache
  19. func (d *Dao) pingMC(c context.Context) (err error) {
  20. conn := d.mc.Get(c)
  21. defer conn.Close()
  22. item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcUUIDExpire}
  23. err = conn.Set(&item)
  24. return
  25. }
  26. // ExistsUUIDCache gets uuid from cache.
  27. func (d *Dao) ExistsUUIDCache(c context.Context, biz int64, uuid string) (exist bool, err error) {
  28. var (
  29. conn = d.mc.Get(c)
  30. key = uuidKey(biz, uuid)
  31. )
  32. defer conn.Close()
  33. if _, err = conn.Get(key); err != nil {
  34. if err == memcache.ErrNotFound {
  35. err = nil
  36. return
  37. }
  38. PromError("mc:get uuid")
  39. log.Error("ExistsUUIDCache() conn.Get(%s) error(%v)", key, err)
  40. return
  41. }
  42. exist = true
  43. return
  44. }
  45. // AddUUIDCache adds uuid cache.
  46. func (d *Dao) AddUUIDCache(c context.Context, biz int64, uuid string) (err error) {
  47. var (
  48. conn = d.mc.Get(c)
  49. key = uuidKey(biz, uuid)
  50. item = &memcache.Item{Key: key, Value: []byte{}, Expiration: d.mcUUIDExpire}
  51. )
  52. defer conn.Close()
  53. if err = conn.Set(item); err != nil {
  54. PromError("mc:add uuid")
  55. log.Error("AddUUIDCache() conn.Set(%+v) error(%v)", item, err)
  56. }
  57. return
  58. }
  59. // DelUUIDCache delete uuid cache.
  60. func (d *Dao) DelUUIDCache(c context.Context, biz int64, uuid string) (err error) {
  61. var (
  62. conn = d.mc.Get(c)
  63. key = uuidKey(biz, uuid)
  64. )
  65. defer conn.Close()
  66. if err = conn.Delete(key); err != nil {
  67. PromError("mc:del uuid")
  68. log.Error("DelUUIDCache(%s) error(%v)", key, err)
  69. }
  70. return
  71. }
  72. // ExistsCDCache gets cd from cache.
  73. func (d *Dao) ExistsCDCache(ctx context.Context, app, mid int64) (exist bool, err error) {
  74. var (
  75. conn = d.mc.Get(ctx)
  76. key = cdKey(app, mid)
  77. )
  78. defer conn.Close()
  79. if _, err = conn.Get(key); err != nil {
  80. if err == memcache.ErrNotFound {
  81. err = nil
  82. return
  83. }
  84. PromError("mc:get cd")
  85. log.Error("ExistsCDCache() conn.Get(%s) error(%v)", key, err)
  86. return
  87. }
  88. exist = true
  89. return
  90. }
  91. // AddCDCache adds cd cache.
  92. func (d *Dao) AddCDCache(ctx context.Context, app, mid int64) (err error) {
  93. var (
  94. conn = d.mc.Get(ctx)
  95. key = cdKey(app, mid)
  96. item = &memcache.Item{Key: key, Value: []byte{}, Expiration: d.mcCDExpire}
  97. )
  98. defer conn.Close()
  99. if err = conn.Set(item); err != nil {
  100. PromError("mc:add cd")
  101. log.Error("AddCDCache() conn.Set(%+v) error(%v)", item, err)
  102. }
  103. return
  104. }