mc_wallet.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/live/wallet/model"
  6. mc "go-common/library/cache/memcache"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _walletMcKey = "wu:%d" // 钱包数据的mc缓存
  12. )
  13. func mcKey(uid int64) string {
  14. return fmt.Sprintf(_walletMcKey, uid)
  15. }
  16. func (d *Dao) CacheVersion(c context.Context) int32 {
  17. return 1
  18. }
  19. func (d *Dao) IsNewVersion(c context.Context, detail *model.McDetail) bool {
  20. return detail.Version == d.CacheVersion(c)
  21. }
  22. // WalletCache 获取钱包缓存
  23. func (d *Dao) WalletCache(c context.Context, uid int64) (detail *model.McDetail, err error) {
  24. key := mcKey(uid)
  25. conn := d.mc.Get(c)
  26. defer conn.Close()
  27. r, err := conn.Get(key)
  28. if err != nil {
  29. if err == mc.ErrNotFound {
  30. return
  31. }
  32. log.Error("[dao.mc_wallet|WalletCache] conn.Get(%s) error(%v)", key, err)
  33. err = ecode.ServerErr
  34. return
  35. }
  36. detail = &model.McDetail{}
  37. if err = conn.Scan(r, detail); err != nil {
  38. log.Error("[dao.mc_wallet|WalletCache] conn.Scan(%s) error(%v)", string(r.Value), err)
  39. }
  40. return
  41. }
  42. // SetWalletCache 设置钱包缓存
  43. func (d *Dao) SetWalletCache(c context.Context, detail *model.McDetail, expire int32) (err error) {
  44. key := mcKey(detail.Detail.Uid)
  45. conn := d.mc.Get(c)
  46. defer conn.Close()
  47. if err = conn.Set(&mc.Item{
  48. Key: key,
  49. Object: detail,
  50. Flags: mc.FlagProtobuf,
  51. Expiration: expire,
  52. }); err != nil {
  53. log.Error("[dao.mc_wallet|SetWalletCache] conn.Set(%s, %v) error(%v)", key, detail, err)
  54. }
  55. return
  56. }
  57. // DelWalletCache 删除等级缓存
  58. func (d *Dao) DelWalletCache(c context.Context, uid int64) (err error) {
  59. key := mcKey(uid)
  60. conn := d.mc.Get(c)
  61. defer conn.Close()
  62. if err = conn.Delete(key); err == mc.ErrNotFound {
  63. return
  64. }
  65. if err != nil {
  66. log.Error("[dao.mc_wallet|DelWalletCache] conn.Delete(%s) error(%v)", key, err)
  67. }
  68. return
  69. }