dao.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. account "go-common/app/service/main/account/api"
  6. "go-common/app/service/main/ugcpay-rank/internal/conf"
  7. "go-common/app/service/main/ugcpay-rank/internal/model"
  8. "go-common/library/cache/memcache"
  9. xsql "go-common/library/database/sql"
  10. "github.com/bluele/gcache"
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. mc *memcache.Pool
  15. db *xsql.DB
  16. accountAPI account.AccountClient
  17. // local cache
  18. elecAVRankLC gcache.Cache
  19. elecUPRankLC gcache.Cache
  20. }
  21. // New init mysql db
  22. func New() (dao *Dao) {
  23. dao = &Dao{
  24. mc: memcache.NewPool(conf.Conf.Memcache),
  25. db: xsql.NewMySQL(conf.Conf.MySQL),
  26. elecAVRankLC: gcache.New(conf.Conf.LocalCache.ElecAVRankSize).LFU().Build(),
  27. elecUPRankLC: gcache.New(conf.Conf.LocalCache.ElecUPRankSize).LFU().Build(),
  28. }
  29. var err error
  30. if dao.accountAPI, err = account.NewClient(conf.Conf.AccountGRPC); err != nil {
  31. panic(err)
  32. }
  33. return
  34. }
  35. // Close close the resource.
  36. func (d *Dao) Close() {
  37. d.mc.Close()
  38. d.db.Close()
  39. }
  40. // Ping dao ping
  41. func (d *Dao) Ping(ctx context.Context) error {
  42. return nil
  43. }
  44. func elecUPRankKey(upMID int64, ver int64) string {
  45. return fmt.Sprintf("ur_eur_%d_%d", ver, upMID)
  46. }
  47. func elecPrepUPRankKey(upMID int64, ver int64) string {
  48. return fmt.Sprintf("ur_epur_%d_%d", ver, upMID)
  49. }
  50. func elecAVRankKey(avID int64, ver int64) string {
  51. return fmt.Sprintf("ur_ear_%d_%d", ver, avID)
  52. }
  53. func elecPrepAVRankKey(avID int64, ver int64) string {
  54. return fmt.Sprintf("ur_epar_%d_%d", ver, avID)
  55. }
  56. //go:generate $GOPATH/src/go-common/app/tool/cache/mc
  57. type _mc interface {
  58. //mc: -key=elecUPRankKey -type=get
  59. CacheElecUPRank(c context.Context, mid int64) (*model.RankElecUPProto, error)
  60. //mc: -key=elecUPRankKey -expire=conf.Conf.CacheTTL.ElecUPRankTTL -encode=json
  61. AddCacheElecUPRank(c context.Context, mid int64, value *model.RankElecUPProto) error
  62. //mc: -key=elecUPRankKey
  63. DelCacheElecUPRank(c context.Context, mid int64) error
  64. //mc: -key=elecAVRankKey -type=get
  65. CacheElecAVRank(c context.Context, avID int64) (*model.RankElecAVProto, error)
  66. //mc: -key=elecAVRankKey -expire=conf.Conf.CacheTTL.ElecAVRankTTL -encode=json
  67. AddCacheElecAVRank(c context.Context, avID int64, value *model.RankElecAVProto) error
  68. //mc: -key=elecAVRankKey
  69. DelCacheElecAVRank(c context.Context, avID int64) error
  70. //mc: -key=elecPrepUPRankKey -type=get
  71. CacheElecPrepUPRank(c context.Context, mid int64) (*model.RankElecPrepUPProto, error)
  72. //mc: -key=elecPrepUPRankKey -expire=conf.Conf.CacheTTL.ElecPrepUPRankTTL -encode=json
  73. AddCacheElecPrepUPRank(c context.Context, mid int64, value *model.RankElecPrepUPProto) error
  74. //mc: -key=elecPrepUPRankKey
  75. DelCacheElecPrepUPRank(c context.Context, mid int64) error
  76. //mc: -key=elecPrepAVRankKey -type=get
  77. CacheElecPrepAVRank(c context.Context, avID int64) (*model.RankElecPrepAVProto, error)
  78. //mc: -key=elecPrepAVRankKey -expire=conf.Conf.CacheTTL.ElecPrepAVRankTTL -encode=json
  79. AddCacheElecPrepAVRank(c context.Context, avID int64, value *model.RankElecPrepAVProto) error
  80. //mc: -key=elecPrepAVRankKey
  81. DelCacheElecPrepAVRank(c context.Context, avID int64) error
  82. }