memcache.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/coupon/model"
  6. gmc "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _prefixCoupons = "cs:%d:%d"
  12. _prefixUseUnique = "cu:%s:%d"
  13. _prefixCouponBlance = "cbl:%d:%d"
  14. _prefixGrantUnique = "gr:%s:%d"
  15. _prefixBranchCount = "bcu:%s"
  16. _useLockTimeout = 10
  17. _prefixCouponAllowances = "cas:%d:%d"
  18. _receiveLog = "rl:%s%s%d"
  19. _uniqueNo = "uq:%s"
  20. _useUniqueNoTimeout = 1296000 //15天
  21. _prefixprizeCard = "nypc:%d:%d:%d" // 元旦活动卡片
  22. _prefixprizeCards = "nypcs:%d:%d" // 元旦活动卡片列表
  23. )
  24. func receiveLogKey(appkey, orderNo string, ct int8) string {
  25. return fmt.Sprintf(_receiveLog, appkey, orderNo, ct)
  26. }
  27. func couponsKey(mid int64, ct int8) string {
  28. return fmt.Sprintf(_prefixCoupons, ct, mid)
  29. }
  30. func useUniqueKey(orderNO string, ct int8) string {
  31. return fmt.Sprintf(_prefixUseUnique, orderNO, ct)
  32. }
  33. func couponBalancesKey(mid int64, ct int8) string {
  34. return fmt.Sprintf(_prefixCouponBlance, ct, mid)
  35. }
  36. func userGrantKey(token string, mid int64) string {
  37. return fmt.Sprintf(_prefixGrantUnique, token, mid)
  38. }
  39. func branchCurrentCount(token string) string {
  40. return fmt.Sprintf(_prefixBranchCount, token)
  41. }
  42. func couponAllowancesKey(mid int64, state int8) string {
  43. return fmt.Sprintf(_prefixCouponAllowances, mid, state)
  44. }
  45. func prizeCardKey(mid, actID int64, cardType int8) string {
  46. return fmt.Sprintf(_prefixprizeCard, mid, actID, cardType)
  47. }
  48. func prizeCardsKey(mid, actID int64) string {
  49. return fmt.Sprintf(_prefixprizeCards, mid, actID)
  50. }
  51. func couponuniqueNoKey(uniqueno string) string {
  52. return fmt.Sprintf(_uniqueNo, uniqueno)
  53. }
  54. // DelUniqueKey delete use coupon lock cache.
  55. func (d *Dao) DelUniqueKey(c context.Context, orderNO string, ct int8) (err error) {
  56. return d.delCache(c, useUniqueKey(orderNO, ct))
  57. }
  58. // DelCouponsCache delete user coupons cache.
  59. func (d *Dao) DelCouponsCache(c context.Context, mid int64, ct int8) (err error) {
  60. return d.delCache(c, couponsKey(mid, ct))
  61. }
  62. // DelCouponBalancesCache delete user coupons blance cache.
  63. func (d *Dao) DelCouponBalancesCache(c context.Context, mid int64, ct int8) (err error) {
  64. return d.delCache(c, couponBalancesKey(mid, ct))
  65. }
  66. // DelGrantKey delete user grant lock cache.
  67. func (d *Dao) DelGrantKey(c context.Context, token string, mid int64) (err error) {
  68. return d.delCache(c, userGrantKey(token, mid))
  69. }
  70. // DelBranchCurrentCountKey delete branch current cache.
  71. func (d *Dao) DelBranchCurrentCountKey(c context.Context, token string) (err error) {
  72. return d.delCache(c, branchCurrentCount(token))
  73. }
  74. // DelCouponAllowancesKey delete allowances cache.
  75. func (d *Dao) DelCouponAllowancesKey(c context.Context, mid int64, state int8) (err error) {
  76. return d.delCache(c, couponAllowancesKey(mid, state))
  77. }
  78. // DelPrizeCardKey .
  79. func (d *Dao) DelPrizeCardKey(c context.Context, mid, actID int64, cardType int8) (err error) {
  80. return d.delCache(c, prizeCardKey(mid, actID, cardType))
  81. }
  82. // DelPrizeCardsKey .
  83. func (d *Dao) DelPrizeCardsKey(c context.Context, mid, actID int64) (err error) {
  84. return d.delCache(c, prizeCardsKey(mid, actID))
  85. }
  86. // CouponsCache coupons cache.
  87. func (d *Dao) CouponsCache(c context.Context, mid int64, ct int8) (coupons []*model.CouponInfo, err error) {
  88. var (
  89. key = couponsKey(mid, ct)
  90. item *gmc.Item
  91. )
  92. conn := d.mc.Get(c)
  93. defer conn.Close()
  94. item, err = conn.Get(key)
  95. if err != nil {
  96. if err == gmc.ErrNotFound {
  97. err = nil
  98. return
  99. }
  100. err = errors.Wrapf(err, "mc.Get(%s)", key)
  101. d.errProm.Incr("get_mc")
  102. return
  103. }
  104. couponInfoList := &model.PointInfoList{}
  105. if err = conn.Scan(item, couponInfoList); err != nil {
  106. err = errors.Wrapf(err, "mc.Scan(%s)", key)
  107. d.errProm.Incr("scan_mc")
  108. return
  109. }
  110. coupons = couponInfoList.PointInfoList
  111. if coupons == nil {
  112. coupons = []*model.CouponInfo{}
  113. }
  114. return
  115. }
  116. // SetCouponsCache set coupons cache.
  117. func (d *Dao) SetCouponsCache(c context.Context, mid int64, ct int8, coupons []*model.CouponInfo) (err error) {
  118. var (
  119. expire = d.mcExpire
  120. key = couponsKey(mid, ct)
  121. )
  122. conn := d.mc.Get(c)
  123. defer conn.Close()
  124. item := &gmc.Item{Key: key, Object: &model.PointInfoList{PointInfoList: coupons}, Expiration: expire, Flags: gmc.FlagProtobuf}
  125. if err = conn.Set(item); err != nil {
  126. err = errors.Wrapf(err, "mc.Set(%s)", key)
  127. d.errProm.Incr("set_mc")
  128. }
  129. return
  130. }
  131. // AddUseUniqueLock add coupon use lock.
  132. func (d *Dao) AddUseUniqueLock(c context.Context, orderNO string, ct int8) (succeed bool) {
  133. var (
  134. key = useUniqueKey(orderNO, ct)
  135. conn = d.mc.Get(c)
  136. err error
  137. )
  138. defer conn.Close()
  139. item := &gmc.Item{
  140. Key: key,
  141. Value: []byte("0"),
  142. Expiration: _useLockTimeout,
  143. }
  144. if err = conn.Add(item); err != nil {
  145. if err != gmc.ErrNotStored {
  146. log.Error("mc.Add(%s) error(%v)", key, err)
  147. d.errProm.Incr("add_mc")
  148. }
  149. } else {
  150. succeed = true
  151. }
  152. return
  153. }
  154. // AddReceiveUniqueLock add coupon use lock.
  155. func (d *Dao) AddReceiveUniqueLock(c context.Context, appkey, orderNO string, ct int8) (succeed bool) {
  156. var (
  157. key = receiveLogKey(appkey, orderNO, ct)
  158. conn = d.mc.Get(c)
  159. err error
  160. )
  161. defer conn.Close()
  162. item := &gmc.Item{
  163. Key: key,
  164. Value: []byte("0"),
  165. Expiration: _useLockTimeout,
  166. }
  167. if err = conn.Add(item); err != nil {
  168. if err != gmc.ErrNotStored {
  169. log.Error("mc.Add(%s) error(%v)", key, err)
  170. d.errProm.Incr("add_mc")
  171. }
  172. } else {
  173. succeed = true
  174. }
  175. return
  176. }
  177. //DelReceiveUniqueLock del receive lock.
  178. func (d *Dao) DelReceiveUniqueLock(c context.Context, appkey, orderNO string, ct int8) (err error) {
  179. err = d.delCache(c, receiveLogKey(appkey, orderNO, ct))
  180. return
  181. }
  182. // DelCache del cache.
  183. func (d *Dao) delCache(c context.Context, key string) (err error) {
  184. conn := d.mc.Get(c)
  185. defer conn.Close()
  186. if err = conn.Delete(key); err != nil {
  187. if err == gmc.ErrNotFound {
  188. err = nil
  189. } else {
  190. err = errors.Wrapf(err, "mc.Delete(%s)", key)
  191. d.errProm.Incr("del_mc")
  192. }
  193. }
  194. return
  195. }
  196. // CouponBlanceCache coupon blance cache.
  197. func (d *Dao) CouponBlanceCache(c context.Context, mid int64, ct int8) (coupons []*model.CouponBalanceInfo, err error) {
  198. var (
  199. key = couponBalancesKey(mid, ct)
  200. item *gmc.Item
  201. )
  202. conn := d.mc.Get(c)
  203. defer conn.Close()
  204. item, err = conn.Get(key)
  205. if err != nil {
  206. if err == gmc.ErrNotFound {
  207. err = nil
  208. return
  209. }
  210. err = errors.Wrapf(err, "mc.Get(%s)", key)
  211. d.errProm.Incr("get_mc")
  212. return
  213. }
  214. couponBlanceList := &model.CouponBalanceList{}
  215. if err = conn.Scan(item, couponBlanceList); err != nil {
  216. err = errors.Wrapf(err, "mc.Scan(%s)", key)
  217. d.errProm.Incr("scan_mc")
  218. return
  219. }
  220. coupons = couponBlanceList.CouponBalanceList
  221. if coupons == nil {
  222. coupons = []*model.CouponBalanceInfo{}
  223. }
  224. return
  225. }
  226. // SetCouponBlanceCache set coupon blance cache.
  227. func (d *Dao) SetCouponBlanceCache(c context.Context, mid int64, ct int8, coupons []*model.CouponBalanceInfo) (err error) {
  228. var (
  229. expire = d.mcExpire
  230. key = couponBalancesKey(mid, ct)
  231. )
  232. conn := d.mc.Get(c)
  233. defer conn.Close()
  234. item := &gmc.Item{Key: key, Object: &model.CouponBalanceList{CouponBalanceList: coupons}, Expiration: expire, Flags: gmc.FlagProtobuf}
  235. if err = conn.Set(item); err != nil {
  236. err = errors.Wrapf(err, "mc.Set(%s)", key)
  237. d.errProm.Incr("set_mc")
  238. }
  239. return
  240. }
  241. // AddUniqueNoLock add grant coupon use lock.
  242. func (d *Dao) AddUniqueNoLock(c context.Context, uniqueno string) (succeed bool) {
  243. var (
  244. key = couponuniqueNoKey(uniqueno)
  245. conn = d.mc.Get(c)
  246. err error
  247. )
  248. defer conn.Close()
  249. item := &gmc.Item{
  250. Key: key,
  251. Value: []byte("0"),
  252. Expiration: _useUniqueNoTimeout,
  253. }
  254. if err = conn.Add(item); err != nil {
  255. if err != gmc.ErrNotStored {
  256. log.Error("mc.Add(%s) error(%v)", key, err)
  257. d.errProm.Incr("add_mc")
  258. }
  259. } else {
  260. succeed = true
  261. }
  262. return
  263. }
  264. // AddGrantUniqueLock add grant unique coupon use lock.
  265. func (d *Dao) AddGrantUniqueLock(c context.Context, token string, mid int64) (succeed bool) {
  266. var (
  267. key = userGrantKey(token, mid)
  268. conn = d.mc.Get(c)
  269. err error
  270. )
  271. defer conn.Close()
  272. item := &gmc.Item{
  273. Key: key,
  274. Value: []byte("0"),
  275. Expiration: _useLockTimeout,
  276. }
  277. if err = conn.Add(item); err != nil {
  278. if err != gmc.ErrNotStored {
  279. log.Error("mc.Add(%s) error(%v)", key, err)
  280. d.errProm.Incr("add_mc")
  281. }
  282. } else {
  283. succeed = true
  284. }
  285. return
  286. }
  287. //BranchCurrentCountCache branchInfo current count cache.
  288. func (d *Dao) BranchCurrentCountCache(c context.Context, token string) (count int, err error) {
  289. var (
  290. key = branchCurrentCount(token)
  291. conn = d.mc.Get(c)
  292. item *gmc.Item
  293. )
  294. defer conn.Close()
  295. if item, err = conn.Get(key); err != nil {
  296. if err == gmc.ErrNotFound {
  297. err = nil
  298. count = -1
  299. return
  300. }
  301. err = errors.Wrapf(err, "conn.Get(%s)", key)
  302. return
  303. }
  304. if err = conn.Scan(item, &count); err != nil {
  305. err = errors.Wrapf(err, "conn.Scan(%+v)", item)
  306. return
  307. }
  308. return
  309. }
  310. // SetBranchCurrentCountCache set branch current cache.
  311. func (d *Dao) SetBranchCurrentCountCache(c context.Context, token string, count int) (err error) {
  312. var (
  313. key = branchCurrentCount(token)
  314. conn = d.mc.Get(c)
  315. )
  316. defer conn.Close()
  317. if err = conn.Set(&gmc.Item{Key: key, Object: count, Flags: gmc.FlagJSON, Expiration: d.mcExpire}); err != nil {
  318. err = errors.Wrapf(err, "conn.Set(%s,%+v)", key, count)
  319. return
  320. }
  321. return
  322. }
  323. // IncreaseBranchCurrentCountCache increase branch current count cache.
  324. func (d *Dao) IncreaseBranchCurrentCountCache(c context.Context, token string, count uint64) (err error) {
  325. var (
  326. key = branchCurrentCount(token)
  327. conn = d.mc.Get(c)
  328. )
  329. defer conn.Close()
  330. if _, err = conn.Increment(key, count); err != nil {
  331. err = errors.Wrapf(err, "conn.Increment(%s,%d)", key, count)
  332. return
  333. }
  334. return
  335. }
  336. // CouponAllowanceCache coupon allowance cache.
  337. func (d *Dao) CouponAllowanceCache(c context.Context, mid int64, state int8) (coupons []*model.CouponAllowanceInfo, err error) {
  338. var (
  339. key = couponAllowancesKey(mid, state)
  340. item *gmc.Item
  341. )
  342. conn := d.mc.Get(c)
  343. defer conn.Close()
  344. item, err = conn.Get(key)
  345. if err != nil {
  346. if err == gmc.ErrNotFound {
  347. err = nil
  348. coupons = nil
  349. return
  350. }
  351. err = errors.Wrapf(err, "mc.Get(%s)", key)
  352. d.errProm.Incr("get_mc")
  353. return
  354. }
  355. couponAllowanceList := &model.CouponAllowanceList{}
  356. if err = conn.Scan(item, couponAllowanceList); err != nil {
  357. err = errors.Wrapf(err, "mc.Scan(%s)", key)
  358. d.errProm.Incr("scan_mc")
  359. return
  360. }
  361. coupons = couponAllowanceList.CouponAllowanceList
  362. if coupons == nil {
  363. coupons = []*model.CouponAllowanceInfo{}
  364. }
  365. return
  366. }
  367. // SetCouponAllowanceCache set coupon allowance cache.
  368. func (d *Dao) SetCouponAllowanceCache(c context.Context, mid int64, state int8, coupons []*model.CouponAllowanceInfo) (err error) {
  369. var (
  370. expire = d.mcExpire
  371. key = couponAllowancesKey(mid, state)
  372. )
  373. conn := d.mc.Get(c)
  374. defer conn.Close()
  375. item := &gmc.Item{Key: key, Object: &model.CouponAllowanceList{CouponAllowanceList: coupons}, Expiration: expire, Flags: gmc.FlagProtobuf}
  376. if err = conn.Set(item); err != nil {
  377. err = errors.Wrapf(err, "mc.Set(%s)", key)
  378. d.errProm.Incr("set_mc")
  379. }
  380. return
  381. }
  382. // SetPrizeCardCache .
  383. func (d *Dao) SetPrizeCardCache(c context.Context, mid, actID int64, prizeCard *model.PrizeCardRep) (err error) {
  384. var (
  385. expire = d.prizeExpire
  386. key = prizeCardKey(mid, actID, prizeCard.CardType)
  387. )
  388. conn := d.mc.Get(c)
  389. defer conn.Close()
  390. item := &gmc.Item{Key: key, Object: prizeCard, Expiration: expire, Flags: gmc.FlagJSON}
  391. if err = conn.Set(item); err != nil {
  392. err = errors.Wrapf(err, "mc.Set(%s)", key)
  393. d.errProm.Incr("set_mc")
  394. }
  395. return
  396. }
  397. // SetPrizeCardsCache .
  398. func (d *Dao) SetPrizeCardsCache(c context.Context, mid, actID int64, prizeCards []*model.PrizeCardRep) (err error) {
  399. var (
  400. expire = d.prizeExpire
  401. key = prizeCardsKey(mid, actID)
  402. )
  403. conn := d.mc.Get(c)
  404. defer conn.Close()
  405. item := &gmc.Item{Key: key, Object: &model.PrizeCards{List: prizeCards}, Expiration: expire, Flags: gmc.FlagJSON}
  406. if err = conn.Set(item); err != nil {
  407. err = errors.Wrapf(err, "mc.Set(%s)", key)
  408. d.errProm.Incr("set_mc")
  409. }
  410. return
  411. }
  412. // PrizeCardCache .
  413. func (d *Dao) PrizeCardCache(c context.Context, mid, actID int64, cardType int8) (prizeCard *model.PrizeCardRep, err error) {
  414. var (
  415. key = prizeCardKey(mid, actID, cardType)
  416. item *gmc.Item
  417. )
  418. conn := d.mc.Get(c)
  419. defer conn.Close()
  420. item, err = conn.Get(key)
  421. if err != nil {
  422. if err == gmc.ErrNotFound {
  423. err = nil
  424. return
  425. }
  426. err = errors.Wrapf(err, "mc.Get(%s)", key)
  427. d.errProm.Incr("get_mc")
  428. return
  429. }
  430. prizeCard = &model.PrizeCardRep{}
  431. if err = conn.Scan(item, &prizeCard); err != nil {
  432. err = errors.Wrapf(err, "mc.Scan(%s)", key)
  433. d.errProm.Incr("scan_mc")
  434. return
  435. }
  436. return
  437. }
  438. // PrizeCardsCache .
  439. func (d *Dao) PrizeCardsCache(c context.Context, mid, actID int64) (prizeCards []*model.PrizeCardRep, err error) {
  440. var (
  441. key = prizeCardsKey(mid, actID)
  442. item *gmc.Item
  443. )
  444. conn := d.mc.Get(c)
  445. defer conn.Close()
  446. item, err = conn.Get(key)
  447. if err != nil {
  448. if err == gmc.ErrNotFound {
  449. err = nil
  450. return
  451. }
  452. err = errors.Wrapf(err, "mc.Get(%s)", key)
  453. d.errProm.Incr("get_mc")
  454. return
  455. }
  456. PrizeCardlist := &model.PrizeCards{}
  457. if err = conn.Scan(item, PrizeCardlist); err != nil {
  458. err = errors.Wrapf(err, "mc.Scan(%s)", key)
  459. d.errProm.Incr("scan_mc")
  460. return
  461. }
  462. prizeCards = PrizeCardlist.List
  463. if prizeCards == nil {
  464. prizeCards = []*model.PrizeCardRep{}
  465. }
  466. return
  467. }