memcache.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/job/main/vip/model"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _vipInfo = "vo:%d"
  13. _vipMadel = "madel:%d"
  14. _vipbuy = "vipbuy:%d"
  15. _vipfrozen = "vipfrozen:%d"
  16. madelExpired = 3600 * 24 * 15
  17. vipbuyExpired = 3600 * 24 * 8
  18. vipFrozenExpired = 60 * 30
  19. vipFrozenFlag = 1
  20. _prefixInfo = "i:"
  21. )
  22. func vipfrozen(mid int64) string {
  23. return fmt.Sprintf(_vipfrozen, mid)
  24. }
  25. func vipbuy(mid int64) string {
  26. return fmt.Sprintf(_vipbuy, mid)
  27. }
  28. func vipInfoKey(mid int64) string {
  29. return fmt.Sprintf(_vipInfo, mid)
  30. }
  31. func vipMadel(mid int64) string {
  32. return fmt.Sprintf(_vipMadel, mid)
  33. }
  34. func keyInfo(mid int64) string {
  35. return _prefixInfo + strconv.FormatInt(mid, 10)
  36. }
  37. //SetVipFrozen .
  38. func (d *Dao) SetVipFrozen(c context.Context, mid int64) (err error) {
  39. var (
  40. key = vipfrozen(mid)
  41. )
  42. conn := d.mc.Get(c)
  43. defer conn.Close()
  44. item := &memcache.Item{Key: key, Object: vipFrozenFlag, Expiration: vipFrozenExpired, Flags: memcache.FlagJSON}
  45. if err = conn.Set(item); err != nil {
  46. err = errors.Wrapf(err, "d.setVipFrozen")
  47. d.errProm.Incr("vip frozen_mc")
  48. return
  49. }
  50. return
  51. }
  52. //DelVipFrozen .
  53. func (d *Dao) DelVipFrozen(c context.Context, mid int64) (err error) {
  54. return d.delCache(c, vipfrozen(mid))
  55. }
  56. //SetVipMadelCache set vip madel cache
  57. func (d *Dao) SetVipMadelCache(c context.Context, mid int64, val int64) (err error) {
  58. var (
  59. key = vipMadel(mid)
  60. )
  61. conn := d.mc.Get(c)
  62. defer conn.Close()
  63. item := &memcache.Item{Key: key, Object: val, Expiration: madelExpired, Flags: memcache.FlagJSON}
  64. if err = conn.Set(item); err != nil {
  65. err = errors.Wrap(err, "d.SetVipMadelCache")
  66. d.errProm.Incr("vipmadel_mc")
  67. }
  68. return
  69. }
  70. //GetVipBuyCache get vipbuy cache by key
  71. func (d *Dao) GetVipBuyCache(c context.Context, mid int64) (val int64, err error) {
  72. var (
  73. key = vipbuy(mid)
  74. item *memcache.Item
  75. )
  76. conn := d.mc.Get(c)
  77. defer conn.Close()
  78. if item, err = conn.Get(key); err != nil {
  79. if err == memcache.ErrNotFound {
  80. err = nil
  81. return
  82. }
  83. err = errors.Wrapf(err, "conn.Get(%s)", key)
  84. d.errProm.Incr("vipinfo_mc")
  85. return
  86. }
  87. if err = conn.Scan(item, &val); err != nil {
  88. err = errors.WithStack(err)
  89. d.errProm.Incr("vipMadelCache_mc")
  90. }
  91. return
  92. }
  93. //SetVipBuyCache set vipbuy cache
  94. func (d *Dao) SetVipBuyCache(c context.Context, mid int64, val int64) (err error) {
  95. var (
  96. key = vipbuy(mid)
  97. )
  98. conn := d.mc.Get(c)
  99. defer conn.Close()
  100. item := &memcache.Item{Key: key, Object: val, Expiration: vipbuyExpired, Flags: memcache.FlagJSON}
  101. if err = conn.Set(item); err != nil {
  102. err = errors.Wrap(err, "d.SetVipBuyCache")
  103. d.errProm.Incr("vipbuy_mc")
  104. }
  105. return
  106. }
  107. //GetVipMadelCache get madel info by mid
  108. func (d *Dao) GetVipMadelCache(c context.Context, mid int64) (val int64, err error) {
  109. var (
  110. key = vipMadel(mid)
  111. item *memcache.Item
  112. )
  113. conn := d.mc.Get(c)
  114. defer conn.Close()
  115. if item, err = conn.Get(key); err != nil {
  116. if err == memcache.ErrNotFound {
  117. err = nil
  118. return
  119. }
  120. err = errors.Wrapf(err, "conn.Get(%s)", key)
  121. d.errProm.Incr("vipinfo_mc")
  122. return
  123. }
  124. if err = conn.Scan(item, &val); err != nil {
  125. err = errors.WithStack(err)
  126. d.errProm.Incr("vipMadelCache_mc")
  127. }
  128. return
  129. }
  130. // SetVipInfoCache set vip info cache.
  131. func (d *Dao) SetVipInfoCache(c context.Context, mid int64, v *model.VipInfo) (err error) {
  132. var (
  133. key = vipInfoKey(mid)
  134. )
  135. conn := d.mc.Get(c)
  136. defer conn.Close()
  137. item := &memcache.Item{Key: key, Object: v, Expiration: d.mcExpire, Flags: memcache.FlagProtobuf}
  138. if err = conn.Set(item); err != nil {
  139. err = errors.Wrap(err, "d.SetVipInfo")
  140. d.errProm.Incr("vipinfo_mc")
  141. }
  142. return
  143. }
  144. // DelVipInfoCache del vip info cache.
  145. func (d *Dao) DelVipInfoCache(c context.Context, mid int64) (err error) {
  146. err = d.delCache(c, vipInfoKey(mid))
  147. return
  148. }
  149. // DelInfoCache del vip info cache.
  150. func (d *Dao) DelInfoCache(c context.Context, mid int64) (err error) {
  151. if err = d.delCache(c, keyInfo(mid)); err != nil {
  152. log.Error("del vipinfo cache(mid:%d) error(%+v)", mid, err)
  153. }
  154. return
  155. }
  156. func (d *Dao) delCache(c context.Context, key string) (err error) {
  157. conn := d.mc.Get(c)
  158. defer conn.Close()
  159. if err = conn.Delete(key); err != nil {
  160. if err == memcache.ErrNotFound {
  161. err = nil
  162. } else {
  163. err = errors.Wrapf(err, "conn.Delete(%s)", key)
  164. d.errProm.Incr("del_mc")
  165. }
  166. }
  167. return
  168. }
  169. // AddTransferLock add lock.
  170. func (d *Dao) AddTransferLock(c context.Context, key string) (succeed bool) {
  171. var (
  172. conn = d.mc.Get(c)
  173. )
  174. defer conn.Close()
  175. item := &memcache.Item{
  176. Key: key,
  177. Value: []byte("0"),
  178. Expiration: 3600,
  179. }
  180. if err := conn.Add(item); err != nil {
  181. if err != memcache.ErrNotStored {
  182. log.Error("conn.Add(%s) error(%v)", key, err)
  183. }
  184. } else {
  185. succeed = true
  186. }
  187. return
  188. }