memcache.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/vip/model"
  6. gmc "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _vipInfo = "vo:%d"
  12. _open = "open:%d"
  13. _pointTip = "pointTip:%d:%d"
  14. _sign = "sign:%d"
  15. _vipfrozen = "vipfrozen:%d"
  16. )
  17. func pointTip(mid, id int64) string {
  18. return fmt.Sprintf(_pointTip, mid, id)
  19. }
  20. func openCode(mid int64) string {
  21. return fmt.Sprintf(_open, mid)
  22. }
  23. func vipInfoKey(mid int64) string {
  24. return fmt.Sprintf(_vipInfo, mid)
  25. }
  26. func signVip(mid int64) string {
  27. return fmt.Sprintf(_sign, mid)
  28. }
  29. func vipfrozen(mid int64) string {
  30. return fmt.Sprintf(_vipfrozen, mid)
  31. }
  32. //GetVipFrozen set vip frozen.
  33. func (d *Dao) GetVipFrozen(c context.Context, mid int64) (val int, err error) {
  34. var (
  35. key = vipfrozen(mid)
  36. )
  37. conn := d.mc.Get(c)
  38. defer conn.Close()
  39. item, err := conn.Get(key)
  40. if err != nil {
  41. if err == gmc.ErrNotFound {
  42. err = nil
  43. return
  44. }
  45. err = errors.Wrapf(err, "conn.Get(%s)", key)
  46. d.errProm.Incr("vipfrozen_mc")
  47. return
  48. }
  49. if err = conn.Scan(item, &val); err != nil {
  50. err = errors.WithStack(err)
  51. return
  52. }
  53. return
  54. }
  55. //DelVipFrozen del vip frozen.
  56. func (d *Dao) DelVipFrozen(c context.Context, mid int64) (err error) {
  57. return d.DelCache(c, vipfrozen(mid))
  58. }
  59. // DelVipInfoCache delete vipinfo cache.
  60. func (d *Dao) DelVipInfoCache(c context.Context, mid int64) (err error) {
  61. return d.DelCache(c, vipInfoKey(mid))
  62. }
  63. // pingMC ping memcache.
  64. func (d *Dao) pingMC(c context.Context) (err error) {
  65. conn := d.mc.Get(c)
  66. defer conn.Close()
  67. if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}); err != nil {
  68. log.Error("conn.Store(set, ping, 1) error(%v)", err)
  69. }
  70. return
  71. }
  72. // SetVipInfoCache set vip info cache.
  73. func (d *Dao) SetVipInfoCache(c context.Context, mid int64, v *model.VipInfo) (err error) {
  74. var (
  75. key = vipInfoKey(mid)
  76. )
  77. conn := d.mc.Get(c)
  78. defer conn.Close()
  79. item := &gmc.Item{Key: key, Object: v, Expiration: d.mcExpire, Flags: gmc.FlagProtobuf}
  80. if err = conn.Set(item); err != nil {
  81. err = errors.Wrapf(err, " conn.Set(%s)", key)
  82. d.errProm.Incr("vipinfo_mc")
  83. }
  84. return
  85. }
  86. // VipInfoCache get vip info.
  87. func (d *Dao) VipInfoCache(c context.Context, mid int64) (v *model.VipInfo, err error) {
  88. var (
  89. key = vipInfoKey(mid)
  90. )
  91. conn := d.mc.Get(c)
  92. defer conn.Close()
  93. item, err := conn.Get(key)
  94. if err != nil {
  95. if err == gmc.ErrNotFound {
  96. err = nil
  97. return
  98. }
  99. err = errors.Wrapf(err, "conn.Get(%s)", key)
  100. d.errProm.Incr("vipinfo_mc")
  101. return
  102. }
  103. v = new(model.VipInfo)
  104. if err = conn.Scan(item, v); err != nil {
  105. err = errors.Wrapf(err, "conn.Scan(%s)", key)
  106. }
  107. return
  108. }
  109. //DelCache del cache.
  110. func (d *Dao) DelCache(c context.Context, key string) (err error) {
  111. conn := d.mc.Get(c)
  112. defer conn.Close()
  113. if err = conn.Delete(key); err != nil {
  114. if err == gmc.ErrNotFound {
  115. err = nil
  116. } else {
  117. log.Error("conn.Delete(%s) error(%v)", key, err)
  118. d.errProm.Incr("del_mc")
  119. }
  120. }
  121. return
  122. }
  123. // GetOpenCodeCount get open code count.
  124. func (d *Dao) GetOpenCodeCount(c context.Context, mid int64) (val int, err error) {
  125. var (
  126. key = openCode(mid)
  127. )
  128. conn := d.mc.Get(c)
  129. defer conn.Close()
  130. item, err := conn.Get(key)
  131. if err != nil {
  132. if err == gmc.ErrNotFound {
  133. err = nil
  134. return
  135. }
  136. err = errors.Wrapf(err, "conn.Get(%s)", key)
  137. d.errProm.Incr("opencode_mc")
  138. return
  139. }
  140. if err = conn.Scan(item, &val); err != nil {
  141. err = errors.WithStack(err)
  142. return
  143. }
  144. return
  145. }
  146. //SetOpenCode set open code.
  147. func (d *Dao) SetOpenCode(c context.Context, mid int64, count int) (err error) {
  148. var (
  149. key = openCode(mid)
  150. )
  151. conn := d.mc.Get(c)
  152. defer conn.Close()
  153. item := &gmc.Item{Key: key, Object: count, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
  154. if err = conn.Set(item); err != nil {
  155. err = errors.WithStack(err)
  156. return
  157. }
  158. return
  159. }
  160. //GetPointTip get pointTip.
  161. func (d *Dao) GetPointTip(c context.Context, mid, id int64) (val int, err error) {
  162. var (
  163. key = pointTip(mid, id)
  164. )
  165. conn := d.mc.Get(c)
  166. defer conn.Close()
  167. item, err := conn.Get(key)
  168. if err != nil {
  169. if err == gmc.ErrNotFound {
  170. err = nil
  171. return
  172. }
  173. err = errors.Wrapf(err, "conn.Get(%s)", key)
  174. d.errProm.Incr("opencode_mc")
  175. return
  176. }
  177. if err = conn.Scan(item, &val); err != nil {
  178. return
  179. }
  180. return
  181. }
  182. //SetPointTip set point tip.
  183. func (d *Dao) SetPointTip(c context.Context, mid, id int64, val int, expired int32) (err error) {
  184. var (
  185. key = pointTip(mid, id)
  186. )
  187. conn := d.mc.Get(c)
  188. defer conn.Close()
  189. item := &gmc.Item{Key: key, Object: val, Expiration: expired, Flags: gmc.FlagJSON}
  190. if err = conn.Set(item); err != nil {
  191. err = errors.WithStack(err)
  192. return
  193. }
  194. return
  195. }
  196. //SetSignVip .
  197. func (d *Dao) SetSignVip(c context.Context, mid int64, t int) (err error) {
  198. var (
  199. key = signVip(mid)
  200. )
  201. conn := d.mc.Get(c)
  202. defer conn.Close()
  203. item := &gmc.Item{Key: key, Object: 1, Expiration: int32(t), Flags: gmc.FlagJSON}
  204. if err = conn.Set(item); err != nil {
  205. err = errors.WithStack(err)
  206. return
  207. }
  208. return
  209. }
  210. //GetSignVip .
  211. func (d *Dao) GetSignVip(c context.Context, mid int64) (val int, err error) {
  212. var (
  213. key = signVip(mid)
  214. )
  215. conn := d.mc.Get(c)
  216. defer conn.Close()
  217. item, err := conn.Get(key)
  218. if err != nil {
  219. if err == gmc.ErrNotFound {
  220. err = nil
  221. return
  222. }
  223. err = errors.Wrapf(err, "conn.Get(%s)", key)
  224. d.errProm.Incr("signvip_mc")
  225. return
  226. }
  227. if err = conn.Scan(item, &val); err != nil {
  228. return
  229. }
  230. return
  231. }
  232. // AddTransferLock add lock.
  233. func (d *Dao) AddTransferLock(c context.Context, key string) (succeed bool) {
  234. var (
  235. conn = d.mc.Get(c)
  236. )
  237. defer conn.Close()
  238. item := &gmc.Item{
  239. Key: key,
  240. Value: []byte("0"),
  241. Expiration: 3600,
  242. }
  243. if err := conn.Add(item); err != nil {
  244. if err != gmc.ErrNotStored {
  245. log.Error("conn.Add(%s) error(%v)", key, err)
  246. }
  247. } else {
  248. succeed = true
  249. }
  250. return
  251. }